[英]Binding textblock foreground color not working
xaml code: XAML代码:
<ControlTemplate x:Key="ChkTemplate"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="TemplatedParent" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<ContentPresenter />
</StackPanel>
</ControlTemplate>
<DataTemplate DataType="{x:Type ABC:Info}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"
Margin="0,0,10,5" Foreground="Green"/>
<TextBlock Text="{Binding Channel}"
Margin="3,0,0,0"
Visibility="{Binding Path=Visible,ElementName=View, Converter={StaticResource BooleanConverter}}" />
<TextBlock.Foreground>
<SolidColorBrush Color="{Binding Foreground}" />
</TextBlock.Foreground>
</StackPanel>
</DataTemplate>
<Style TargetType="ListViewItem"
x:Key="SelectedItem">
<Setter Property="Template"
Value="{StaticResource ChkTemplate}" />
</Style>
class: 类:
public class Info : DependencyObject
{
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
}
xaml.cs: xaml.cs:
private readonly RangeObservableCollection<Info> _validInfo;
Info.Foreground = Brushes.Red;
_validInfo.Add(Info);
Above code is not changing foreground color of textblock what am i doing wrong? 上面的代码没有改变textblock的前景色我在做什么错?
i tried your code and in works for me. 我尝试了您的代码,并为我工作。 can you post the code where your datatemplate comes into action?
您可以将代码发布到您的数据模板生效的地方吗? i do it with a listbox.
我用一个列表框来做。
EDIT 编辑
public partial class MainWindow : Window
{
private ObservableCollection<Info> _source;
public MainWindow()
{
this.MySource = new ObservableCollection<Info>();
InitializeComponent();
this.DataContext = this;
this.MySource.Add(new Info(){Foreground = Brushes.Red});
}
public ObservableCollection<Info> MySource
{
get { return _source; }
set { _source = value; }
}
}
public class Info : DependencyObject
{
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register("Foreground", typeof(Brush), typeof(Info));
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
}
xaml XAML
<Grid>
<Grid.Resources>
<DataTemplate DataType="{x:Type TestForeground:Info}">
<TextBlock Text="{Binding}" Foreground="{Binding Foreground}"/>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding MySource}">
</ListBox>
</Grid>
First off, for any future questions, I'd be sure to provide a "complete" example - the code you've pasted in requires a bit of tinkering to even get running. 首先,对于以后的任何问题,我都将确保提供一个“完整”的示例-您粘贴的代码甚至需要重新修改才能运行。
That said, there are a number of typos in there that could be causing problems - the following slightly cleaned up code binds properly: 就是说,这里有许多错别字可能会导致问题-以下经过稍微清理的代码可以正确绑定:
XAML: XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanConverter"/>
<ControlTemplate x:Key="ChkTemplate"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0"
IsChecked="{TemplateBinding IsSelected}"/>
<ContentPresenter />
</StackPanel>
</ControlTemplate>
<DataTemplate DataType="{x:Type WpfApplication1:Info}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"
Margin="0,0,10,5"
Foreground="Green"/>
<TextBlock
Text="{Binding Channel}"
Margin="3,0,0,0"
Visibility="{Binding Path=Visible, Converter={StaticResource BooleanConverter}}"
Foreground="{Binding Foreground}"/>
</StackPanel>
</DataTemplate>
<Style TargetType="ListViewItem" x:Key="{x:Type ListViewItem}">
<Setter Property="Template" Value="{StaticResource ChkTemplate}" />
</Style>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Infos}">
</ListView>
</Grid>
</Window>
Code: 码:
namespace WpfApplication1
{
using System.Collections.ObjectModel;
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private readonly ObservableCollection<Info> _validInfo;
public Window1()
{
_validInfo = new ObservableCollection<Info>();
InitializeComponent();
this.DataContext = this;
var info = new Info();
info.Foreground = Brushes.Red;
info.Visible = true;
info.Channel = "not sure";
_validInfo.Add(info);
info = new Info();
info.Foreground = Brushes.Blue;
info.Visible = true;
info.Channel = "also not sure";
_validInfo.Add(info);
}
public ObservableCollection<Info> Infos { get { return _validInfo; } }
}
public class Info : DependencyObject
{
public Brush Foreground
{
get { return (Brush)GetValue(TextBlock.ForegroundProperty); }
set { SetValue(TextBlock.ForegroundProperty, value); }
}
public bool Visible { get; set; }
public string Channel { get; set; }
public override string ToString()
{
return string.Format("{0}-{1}-{2}", Channel, Foreground, Visible);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.