wpf 如何以编程方式更改 ItemContainerStyle 中的边框背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18428637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:29:39  来源:igfitidea点击:

How to change the Border Background Color in a ItemContainerStyle programmatically?

wpftemplatesstylesfindname

提问by Dirk Schiller

I have a TreeView and it's own Style and it's own ItemContainerStyle. In the ItemContainerStyle I have a Border with the Name "SelectedRectangle". Now I want to change the Background Color of this "SelectedRectangle" by using this Code (I found it in the Internet):

我有一个 TreeView,它有自己的 Style 和它自己的 ItemContainerStyle。在 ItemContainerStyle 我有一个名为“SelectedRectangle”的边框。现在我想使用此代码更改此“SelectedRectangle”的背景颜色(我在 Internet 上找到了它):

    Border brd = (Border)lstDbTree.Template.FindName("SelectedRectangle", lstDbTree); //dosnt work - returns 'null'
    brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#7B7A7C")); // Null Pointer Exception

I don't know how I can get access to the ItemContainerStyle XAML to manipulate it programmatically.

我不知道如何访问 ItemContainerStyle XAML 来以编程方式操作它。

The Code:

编码:

Shell.xaml

外壳.xaml

    <TreeView DockPanel.Dock="Bottom" Name="lstDbTree"
      ...
      ItemContainerStyle="{StaticResource DbTreeItemStyle}"
      ...
    />

CoreStyles.xaml

核心样式.xaml

    <Style TargetType="TreeViewItem" x:Key="DbTreeItemStyle">
      <Setters...>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="TreeViewItem">
            ...
              <Border x:Name="SelectedRectangle" BorderBrush="#44ffffff" BorderThickness="1" Grid.Column="1" CornerRadius="1" IsHitTestVisible="False" Opacity="0" Background="#555355"/>
            ...
          </ControlTemplate>
        </Setter.Value>
      </Style

Shell.xaml.cs

外壳.xaml.cs

    private void ColorB_OnClick(object sender, RoutedEventArgs e)
    {
        Border brd = (Border)lstDbTree.Template.FindName("SelectedRectangle", lstDbTree); //dosnt work
        brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#7B7A7C"));
    }

What I want to do

我想做的事

enter image description here

在此处输入图片说明

Thank's a lot for any Help.

非常感谢任何帮助。

回答by Dirk Schiller

I could fix this Issue with the ItemContainerGenerator:

我可以用 ItemContainerGenerator 解决这个问题:

    private void ColorB_OnClick(object sender, RoutedEventArgs e)
    {
        //TODO: Do this for all Items and not only for the "Selected Item"
        TreeViewItem tvi = lstDbTree.ItemContainerGenerator.ContainerFromItem(lstDbTree.SelectedItem) as TreeViewItem;
        Border brd = (Border)tvi.Template.FindName("SelectedRectangle", tvi);
        brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#7B7A7C"));
    }