Wpf TreeView模板:绑定返回null
我正在尝试使用样式化一个Wpf TreeView模板:绑定返回null,wpf,binding,Wpf,Binding,我正在尝试使用样式化一个TreeView,在我尝试将绑定添加到我的DataContext之前,一切都很正常。也就是说,我将箭头的路径的Fill替换为当前主题的MultiBinding。以下是相关代码: <Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1" Data="M 4 0 L 8 4 L 4 8 Z"> <Path.Fill>
TreeView
,在我尝试将绑定添加到我的DataContext
之前,一切都很正常。也就是说,我将箭头的路径的Fill
替换为当前主题的MultiBinding
。以下是相关代码:
<Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="1" Data="M 4 0 L 8 4 L 4 8 Z">
<Path.Fill>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource ThemeToColorConverter}"
ConverterParameter="Foreground">
<Binding Path="Themes" />
<Binding Path="ThemeIndex" />
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Path.Fill>
</Path>
我在转换器中设置了一个断点,问题似乎是两个绑定
找不到它们的路径
,因为它们都向转换器发送null
。但是,我在代码前面设置按钮
样式时使用了相同的代码,按钮显示得非常完美
我能想到的唯一一件事是,在TreeView样式中,绑定
来自不同的源。我只是不知道他们会怎么做,或者怎么解决。谢谢 好的,我知道了,所以我发布了我的解决方案,希望其他有同样问题的人都能找到它。问题在于绑定绑定到的是TreeView项
,而不是UserControl的DataContext
,因此我将UserControl
命名为并将绑定的元素名
设置为:
<Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="1" Data="M 4 0 L 8 4 L 4 8 Z">
<Path.Fill>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource ThemeToColorConverter}"
ConverterParameter="Foreground">
<Binding ElementName="Control" Path="Themes" />
<Binding ElementName="Control" Path="ThemeIndex" />
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Path.Fill>
</Path>
好的,我知道了,所以我发布了我的解决方案,希望其他有同样问题的人都能找到它。问题在于绑定绑定到的是TreeView项
,而不是UserControl的DataContext
,因此我将UserControl
命名为并将绑定的元素名
设置为:
<Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="1" Data="M 4 0 L 8 4 L 4 8 Z">
<Path.Fill>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource ThemeToColorConverter}"
ConverterParameter="Foreground">
<Binding ElementName="Control" Path="Themes" />
<Binding ElementName="Control" Path="ThemeIndex" />
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Path.Fill>
</Path>
输出窗口中是否存在绑定异常?是的,事实上。此处:System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=Themes;DataItem=null;目标元素是“SolidColorBrush”(HashCode=31542128);目标属性是'Color'(键入'Color')
好的,我想我可能有它。多绑定
正在尝试从每个项
的源
中提取。现在如何修复它…@Andy:顺便说一句,谢谢你的提示。我从来没有想过要看那里,结果它解决了我的问题。输出窗口中有绑定异常吗?是的,事实上。此处:System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=Themes;DataItem=null;目标元素是“SolidColorBrush”(HashCode=31542128);目标属性是'Color'(键入'Color')
好的,我想我可能有它。多绑定
正在尝试从每个项
的源
中提取。现在如何修复它…@Andy:顺便说一句,谢谢你的提示。我从没想过要去那里看看,结果它解决了我的问题。