我有以下问题:我正在设计一个UserControl,一个渐变色仪表。我已经决定使用MVVM设计模式,这是一个不错的选择。但是,我有以下问题。在View XAML文件中,我尝试使用custtom转换器将值转换为颜色,这需要2个参数。为此,我使用MultiBinding:
<ItemsControl ItemsSource="{Binding Path=ViewData}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Height="2">
<Rectangle.Fill>
<MultiBinding Converter="{StaticResource colorConverter}">
<Binding Path="Value"/>
<Binding Source="{StaticResource Palette_ICOS}"/>
</MultiBinding>
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
问题是,我不想使用
{StaticResource Palette_ISO1}
作为第二个参数,但是属性,它是DataContext的直接属性,不是ViewData集合成员的属性。我已经尝试了几种方法来完成这种情况,但没有取得重大成功。
最后,我尝试了以下内容:
<Binding Path="CurrentPallete"/>
和CurrentPallete看起来像:
public Palette CurrentPalette
{
get { return _currentPalette; }
set
{
_currentPalette = value;
}
}
即。类中的属性,其实例设置为主控件的DataContext,该控件承载 ItemControl 。我得到的是
[0x00000001] = {DependencyProperty.UnsetValue}
调试器中的值,当调用相应的转换器时,这可能意味着找不到该属性。任何人都可以指出实现预期效果的方法是什么?非常感谢!
答案 0 :(得分:3)
<Binding Path="DataContext.Palette_ICOS"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}" />
答案 1 :(得分:0)
你试过吗
<Binding Path="Palette_ICOS"/>
如果Palette_ICOS
是当前项DataContext
绑定中的属性,则应该有效。