0

So, I'm building an order tracking app with different user accounts, some of whom have less need-to-know than others. This means that certain controls are displayed for some accounts, and hidden for others.

The datacontext for the Window is set to my Order class, and the data binding within the text fields works perfectly in regards to displaying properties from the specific Order. However, the DataTemplates and Triggers I've made don't seem to be doing anything at all, and I'm not entirely sure why. I've looked all over the web and I can't seem to find why it's not working. Here's the XAML:

    <Label Name="StatusLabelText" Content="Status:" FontSize="15" DockPanel.Dock="Top">
        <Label.Resources>
            <DataTemplate DataType="x:Type local:Order">
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=selectedAccount}" Value="Color Correct">
                        <Setter Property="Visibility" Value="Hidden"></Setter>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Label.Resources>
    </Label>
Steve
  • 6,334
  • 4
  • 39
  • 67
furkle
  • 5,019
  • 1
  • 15
  • 24
  • could it be that `selectedAccount` should be `SelectedAccount`? Usually people have capitalized property, this might be a typo. Then again, it might be right :) – Steve Jan 31 '14 at 18:17
  • Do you see the Get called? I would have a bool and visibility converter. – paparazzo Jan 31 '14 at 18:32
  • One simplier XAML. Two XAML is hard to debug. Three put the business logic in the coded behind. – paparazzo Jan 31 '14 at 19:00

1 Answers1

3

I suspect you want to hide label in case selectedAccount value is Color Correct.

You need Style to do that and not a template if my assumption is correct which can be done like this:

<Label Name="StatusLabelText" Content="Status:" FontSize="15"
       DockPanel.Dock="Top">
   <Label.Style>
      <Style TargetType="Label">
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=selectedAccount}"
                         Value="Color Correct">
                 <Setter Property="Visibility" Value="Collapsed"/>
             </DataTrigger>
         </Style.Triggers>
      </Style>
   </Label.Style>
</Label>

On a side note, you should use Collapsed instead of Hidden to set visibility of control in case you don't want the label to take the size even when it's not visible on GUI. Read more about it here.

Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • 1
    Weirdly enough, I actually figured out pretty much this exact code a few minutes ago the way I normally do - some combination of Googling and beating my head against the problem until it gives. Good point on Collapsed - not sure why I overlooked that. Thanks so much for your time. – furkle Jan 31 '14 at 18:40