1
votes

I'll preface this and say that I'm new to Silverlight development by about week so I'm most likely doing it wrong...

Anyway I have a Label and a TextBox done up thusly in XAML:

<dataInput:Label Target="{Binding ElementName=JobCode}" Height="18" HorizontalAlignment="Left" Margin="15,7,0,0" Name="lableJobCode" VerticalAlignment="Top" Width="250" FontWeight="Bold"  Grid.Column="1" />  
<TextBox Height="23" Text="{Binding SelectedRole.Job_Code}" HorizontalAlignment="Left" Margin="15,31,0,0" Name="JobCode" VerticalAlignment="Top" Width="277" Grid.Column="1" IsReadOnly="{Binding IsNotAdmin}" />   

Everything works great, the only issue I have is that the binding I'm doing on the IsReadOnly attribute which goes to a boolean in my ViewModel which is set based on a call to an authentication service, is now overriding the label Content to the name of my ViewModel property: IsNotAdmin. I can't seem to find a way to specify which data binding source to pull the label content MetaData from. Maybe I'm missing something on how to manipulate control editablity/visibility from my ViewModel.

--Update: The data source class that the TextBox is bound to is as follows (for the relevant parts):

public class RoleSummary {

    [Display(Name= "Job Code (To be Completed by HR):")]
    public string Job_Code { get; set; }  

Without the binding to the IsReadOnly attribute the Label displays the text from the data annotation just fine. When I add the binding it displays "IsNotAdmin"

1
See my edit below, which should take care of this.David Hay

1 Answers

1
votes

can you post more of your code? I'm not entirely sure what it is that you're trying to make happen so it's hard to propose a solution.

I assume you're trying to create a text entry element that has validation performed on it (hence the label) -- but what exactly is the label supposed to be showing for it's content?

EDIT: I figured this out. The label control by default looks through all the properties in its datacontext looking for metadata it can use. For whatever reason it decided to use the metadata for the IsNotAdmin property in your code (even though you didn't set it manually, I assume that the Display metadata gets a default value of the property name), and so you get that for the text of the label.

Microsoft put in a property specifier into the data controls so you can tell it which property it should use for the metadata lookup: PropertyPath

Try it like this:

<dataInput:Label Target="{Binding ElementName=JobCode}" PropertyPath="SelectedRole.Job_Code" Height="18" HorizontalAlignment="Left" Margin="15,7,0,0" Name="lableJobCode" VerticalAlignment="Top" Width="250" FontWeight="Bold"  Grid.Column="1" />  
<TextBox Height="23" Text="{Binding SelectedRole.Job_Code}" HorizontalAlignment="Left" Margin="15,31,0,0" Name="JobCode" VerticalAlignment="Top" Width="277" Grid.Column="1" IsReadOnly="{Binding IsNotAdmin}" />

As long as your datacontext is right (which it should be) this should work for you -- it worked in my sample I reconstructed from your code.