简体   繁体   中英

Having trouble setting visibility of control through DataTemplates

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>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM