2010-02-19 65 views
1

推倒前台我在做:WPF结合与GridViewColumn

<ListView Margin="34,42,42,25" Name="listView1"> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn Width="550" Header="Value" DisplayMemberBinding="{Binding Path=MyValue}"/> 
    </GridView> 
    </ListView.View> 
    <ListView.Resources> 
    <Style TargetType="{x:Type TextBlock}"> 
     <Setter Property="Foreground" Value="Green"/> 
    </Style> 
    </ListView.Resources> 
</ListView> 

,这是工作,我可以看到我的绿色项目。

现在,我想用这种具有约束力的价值,所以我有一个属性:

private Color _theColor; 

public System.Windows.Media.Color TheColor 
{ 
    get { return _theColor; } 
    set 
    { 
     if (_theColor != value) 
     { 
      _theColor = value; 
      OnPropertyChanged("TheColor"); 
     } 
    } 
} 

,但如果我用这个绑定:

<Setter Property="Foreground" Value="{Binding Path=TheColor}"/> 

它不工作...

我该如何纠正?

当然,我的TheColor设置为Colors.Green ...

感谢您的帮助

回答

1

容易,你不能绑定到一个ColorForeground需要设置为Brush。所以我的值设置为SolidColorBrushBrush的颜色属性绑定到你的TheColorDependencyProperty

<Style TargetType="{x:Type TextBlock}"> 
    <Setter Property="Foreground"> 
     <Setter.Value> 
      <SolidColorBrush Color="{Binding Path=TheColor}" /> 
     </Setter.Value> 
    </Setter> 
</Style> 

在我的例子中,我只是绑定的属性TheColorDependencyProperty

public static readonly DependencyProperty TheColorProperty = 
DependencyProperty.Register("TheColor", typeof(System.Windows.Media.Color), typeof(YourWindow)); 

public System.Windows.Media.Color TheColor 
{ 
    get { return (System.Windows.Media.Color)GetValue(TheColorProperty); } 
    set { SetValue(TheColorProperty, value); } 
} 

后那你可以绑定到TheColorDependencyProperty。在我的情况下,我只是给主窗口/用户控制/页面一个x:名称并绑定到:

<SolidColorBrush Color="{Binding Path=TheColor, ElementName=yourWindowVar}" /> 
+0

感谢它的工作 – Tim 2010-02-19 16:08:15