2011-11-14 62 views
1

我有以下问题:我正在设计一个UserControl,一个渐变色表。我决定使用MVVM设计模板,这是一个不错的选择。但是,我有以下问题。在View XAML文件中,我尝试使用custtom转换器将值转换为颜色,该转换器需要2个参数。为此,我用的是MultiBinding:如何将DataBinding设置为XAML中的任意属性?

<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

你试过

<Binding Path="Palette_ICOS"/> 

如果Palette_ICOS是在当前项目的DataContext一个属性绑定它应该工作。

3
<Binding Path="DataContext.Palette_ICOS" 
     RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}" /> 
+0

我想你的意思是'路径'那里,也有一些释义的话不会伤害。 –

+0

@ H.B。,是的,我的意思是路径,而不是源... –

+0

正确,那是我需要的解决方案 –

相关问题