Reputation: 2357
I have figured out three different ways to represent a mandatory field label. However I not sure whose use is ideal. One requirement is that the label should support access keys. Please help me understand which of them should be used. Alternative suggestions are also appreciated.
<Label Width="200" HorizontalAlignment="Left" Target="{Binding ElementName=mytb}">
<TextBlock>
<Run Text="*" Foreground="Red" FontWeight="Heavy" />
<AccessText Text="_Name"/>
</TextBlock>
</Label>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Target="{Binding ElementName=mytb}">
<StackPanel Orientation="Horizontal" Grid.Row="17" Grid.Column="0" HorizontalAlignment="Right">
<TextBlock Text="*" Foreground="Red" VerticalAlignment="Center" FontWeight="Heavy"></TextBlock>
<AccessText Text="_Name:" />
</StackPanel>
</Label>
<StackPanel Orientation="Horizontal">
<TextBlock Text="*" Foreground="Red" VerticalAlignment="Center" FontWeight="Heavy"/>
<Label Content="_Name" VerticalAlignment="Center" Target="{Binding ElementName=mytb}"/>
</StackPanel>
Upvotes: 2
Views: 1359
Reputation: 883
"Should be used" is relative. For example we can think in terms of performance. This could mean using a version which would give the smaller visual tree. Normally the Textblock is pretty light on this but lacks the Target property so access keys would not work. From your solutions the third one is better then the second one because it contains directly the StackPanel without the Label wrapped around it. The first solution with the Run text element seems an elegant one and I prefer that. Regarding the functionality you want to achieve I would mark the TextBox itself as mandatory for example with a validation rule on the binding. This way the marking could appear and disappear as the user types. See example : http://www.nbdtech.com/Blog/archive/2010/07/05/wpf-adorners-part-3-ndash-adorners-and-validation.aspx. Also regarding the visual tree and deciding which controls to use I recommend the Snoop tool: http://snoopwpf.codeplex.com.
Upvotes: 0
Reputation: 69985
If I had to choose one of your examples, I would choose the first, but if not, then I would use an Attached Property
for this instead:
<TextBox Text="{Binding SomeProperty}" Attached:TextBoxProperties.IsMandatory="True" />
Of course, you would have the small problem of creating an Attached Property
, but red asterisks (*) are so last era. My IsMandatory
property basically displays a message in the TextBox
when it is empty, but this is WPF... yours could add a red asterisk or anything else that you can imagine. You could even use this Attached Property
to add an asterisk onto the Label
instead of the TextBox
like in my example.
My Attached Property
works in conjunction with my Label
and LabelColour
Attached Properties
which each add extra functionality... the Label
property provides a simple ControlTemplate
(which is the default TextBox
template with an extra TextBlock
) which it reads from the App.xaml
page. The LabelColour
property just lets me select different Foreground
colours for the extra TextBlock
, but is internally set to Red
when using the IsMandatory
property.
My point is this: you could just go with an old red asterisk like so many before you, or you could branch out and utilise the features that WPF provides to come up with a better solution.
Upvotes: 1