0

我有一个分层树视图,其中每个项目都包含一个复杂的实体,其中包括:

边框 -- 复选框 -- TextBlock

每当复选框 IsEnabled 属性设置为“False”时,我想更改边框的不透明度

这就是我所拥有的:

 <HierarchicalDataTemplate DataType="{x:Type sd:LegendTreeViewItem}" ItemsSource="{Binding Path=SubSensors}">
                        <Border x:Name="treeViewItemBorder" Height="24" Margin="3 3 3 3" Width="350" Background="{Binding Path=Color}" CornerRadius="8 8 8 8">
                            <StackPanel Orientation="Horizontal">
                                <CheckBox  x:Name="treeViewItemCheckbox" Margin="5 5 5 5" Click="CheckBox_Click" IsChecked="{Binding IsChecked}" VerticalAlignment="Center" IsEnabled="{Binding Enabled}"  Style="{StaticResource OpacityOnDisabled}"/>
                                <TextBlock Height="Auto" FontFamily="Tahoma" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" Text="{Binding Path=Name}" 
                                           VerticalAlignment="Center" ToolTip="{Binding Path=Name}"/>
                            </StackPanel>  </Border>
                    </HierarchicalDataTemplate>

    <TreeView x:Name="legendTypeTree" Grid.Row="1" Foreground="White" ItemsSource="{Binding  ElementName=uc, Path=TypeItemsSource}">
                            <TreeView.Resources>
                                <Style x:Key="OpacityOnDisabled" TargetType="{Binding RelativeSource={RelativeSource AncestorType=Border}}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsEnabled}" Value="False">
                                            <Setter Property="Border.Opacity" Value="0.3"/>
                                            <Setter Property="Border.Background" Value="White"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TreeView.Resources>
                        </TreeView>
4

1 回答 1

1
  1. 移动 TreeView 资源中边框的 HierarchicalDataTemplate 和 Style。(样式首先是为了StaticResource OpacityOnDisabled正确解决)。

  2. 您想更改边框的样式,因此在复选框上应用样式是没有意义的。从 CheckBox移动Style="{StaticResource OpacityOnDisabled}"到 Border 元素。设置正确的样式类型,并为 DataTrigger 将绑定更改为Binding="{Binding ElementName=treeViewItemCheckbox, Path=IsChecked}"

最后你应该有类似的东西,它应该做你想做的事: 结果:

<TreeView
    x:Name="legendTypeTree"
    Grid.Row="1"
    Foreground="White"
    ItemsSource="{Binding  ElementName=uc, Path=TypeItemsSource}">
    <TreeView.Resources>
        <Style x:Key="OpacityOnDisabled" TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=treeViewItemCheckbox, Path=IsChecked}" Value="False">
                    <Setter Property="Border.Opacity" Value="0.3"/>
                    <Setter Property="Border.Background" Value="White"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <HierarchicalDataTemplate DataType="{x:Type sd:LegendTreeViewItem}" ItemsSource="{Binding Path=SubSensors}">
            <Border
                x:Name="treeViewItemBorder"
                Height="24"
                Margin="3 3 3 3"
                Width="350"
                Background="{Binding Path=Color}"
                CornerRadius="8 8 8 8"
                Style="{StaticResource OpacityOnDisabled}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox
                        x:Name="treeViewItemCheckbox"
                        Margin="5 5 5 5"
                        Click="CheckBox_Click"
                        IsChecked="{Binding IsChecked}"
                        VerticalAlignment="Center"
                        IsEnabled="{Binding Enabled}" />
                    <TextBlock
                        Height="Auto"
                        FontFamily="Tahoma"
                        FontWeight="Bold"
                        Foreground="Black"
                        HorizontalAlignment="Left"
                        Text="{Binding Path=Name}"
                        VerticalAlignment="Center"
                        ToolTip="{Binding Path=Name}"/>
                </StackPanel>
            </Border>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
于 2012-07-19T10:59:17.807 回答