Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将ControlTemplate内部的ColorBrush绑定到ControlTemplate外部的自定义属性?_C#_.net_Wpf_Mvvm - Fatal编程技术网

C# 如何将ControlTemplate内部的ColorBrush绑定到ControlTemplate外部的自定义属性?

C# 如何将ControlTemplate内部的ColorBrush绑定到ControlTemplate外部的自定义属性?,c#,.net,wpf,mvvm,C#,.net,Wpf,Mvvm,这种绑定让我发疯,尝试了stackoverflow的各种解决方案(甚至据说是完全相同的)。我不明白为什么绑定不起作用 问题: 我有一个应用于GridViewColumnHeader的带有ControlTemplate的样式。这是具有ControlTemplate属性的样式: <Style x:Key="GridHeaderWithInkColor" TargetType="{x:Type GridViewColumnHeader}">

这种绑定让我发疯,尝试了stackoverflow的各种解决方案(甚至据说是完全相同的)。我不明白为什么绑定不起作用

问题:

我有一个应用于GridViewColumnHeader的带有ControlTemplate的样式。这是具有ControlTemplate属性的样式:

    <Style x:Key="GridHeaderWithInkColor" TargetType="{x:Type GridViewColumnHeader}">
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
              <Border BorderThickness="0,0,0,1" BorderBrush="White">
                <StackPanel Margin="0,0,0,10" Orientation="Vertical">
                 <TextBlock Text="{TemplateBinding Content}" Padding="5" 
                                        Width="{TemplateBinding Width}" TextAlignment="Left"
                                                   FontSize="24" Foreground="White"/>
                <Rectangle Height="15" Width="70" Margin="10" RadiusX="5" RadiusY="5" StrokeThickness="2" Stroke="DarkGray" HorizontalAlignment="Center">
                  <Rectangle.Fill>
                    <SolidColorBrush Color="???????????????"/>                                                        
                  </Rectangle.Fill>
                </Rectangle>
             </StackPanel>
           </Border>
       </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
(我认为)这两件事可能让我的生活变得困难:

  • SolidColorBrush嵌套在其他控件中
  • 我尝试使用的属性是自定义定义的属性(我确信它定义正确)
如何在使用时将
SolidColorBrush
内部
ControlTemplate
绑定到
GridViewColumn
中的属性
local:ExtendedProps.InkColor

GridViewColumnHeader
有一个
Column
属性,可用于绑定
GridViewColumn
的属性:

Color="{Binding Path=Column.(local:ExtendedProps.InkColor), 
    RelativeSource={RelativeSource AncestorType=GridViewColumnHeader}}"

该列不是标头的可视祖先,因此使用
{RelativeSource AncestorType=GridViewColumn}
将不起作用。

如果
ExtendedProps.InkColor
是正确声明的attched属性,则可以通过
Color=“{Binding Path=(local:ExtendedProps.InkColor)),RelativeSource绑定到该列={RelativeSource AncestorType=GridViewColumn}}“
。请看这里:我已经尝试过了,但它不起作用。也许嵌套有什么作用?请考虑到controltemplate的目标是GridViewColumnHeader,但属性local:ExtendedProps.InkColor是在GridViewColumn中设置的(我假设它是其父属性)。可能属性声明不正确。调试应用程序时,Visual Studio中的输出窗口中是否有任何数据绑定错误消息?我非常确定它是正确的。太棒了。它做到了!
<SolidColorBrush Color="{Binding DataContext.IsItemSelected,
                         RelativeSource={RelativeSource AncestorType=GridViewColumn}}"/>                                                        
Color="{Binding Path=Column.(local:ExtendedProps.InkColor), 
    RelativeSource={RelativeSource AncestorType=GridViewColumnHeader}}"