C# 绑定文本块前景色不工作
xaml代码:C# 绑定文本块前景色不工作,c#,xaml,C#,Xaml,xaml代码: <ControlTemplate x:Key="ChkTemplate" TargetType="ListViewItem"> <StackPanel Orientation="Horizontal"> <CheckBox Margin="0,0,3,0"> &
<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>
xaml.cs:
private readonly RangeObservableCollection<Info> _validInfo;
Info.Foreground = Brushes.Red;
_validInfo.Add(Info);
私有只读范围ObservableCollection\u validInfo;
Info.Foreground=画笔.Red;
_有效信息添加(信息);
上面的代码没有更改textblock的前景色我做错了什么?我尝试了你的代码,正在为我工作。你能在datatemplate开始工作的地方发布代码吗?我用一个列表框 编辑
公共部分类主窗口:窗口
{
私有可观测采集源;
公共主窗口()
{
this.MySource=新的ObservableCollection();
初始化组件();
this.DataContext=this;
this.MySource.Add(new Info(){Foreground=brusks.Red});
}
公共可观测集合MySource
{
获取{return\u source;}
设置{u source=value;}
}
}
公共类信息:DependencyObject
{
public static readonly dependencProperty ForegroundProperty=dependencProperty.Register(“前台”、typeof(画笔)、typeof(信息));
公共画笔前景
{
获取{return(Brush)GetValue(ForegroundProperty);}
set{SetValue(ForegroundProperty,value);}
}
}
xaml
首先,对于将来的任何问题,我一定会提供一个“完整”的示例-您粘贴的代码需要进行一些修补,甚至可以运行 这就是说,其中有许多可能导致问题的拼写错误-以下稍微清理一下的代码绑定正确: XAML:
代码:
命名空间WpfApplication1
{
使用System.Collections.ObjectModel;
///
///Window1.xaml的交互逻辑
///
公共部分类Window1:Window
{
私有只读可观察收集_validInfo;
公共窗口1()
{
_validInfo=新的ObservableCollection();
初始化组件();
this.DataContext=this;
var info=新信息();
info.Foreground=画笔.Red;
info.Visible=true;
info.Channel=“不确定”;
_有效信息添加(信息);
信息=新信息();
info.Foreground=brusks.Blue;
info.Visible=true;
info.Channel=“也不确定”;
_有效信息添加(信息);
}
公共ObservableCollection信息{get{return}validInfo;}
}
公共类信息:DependencyObject
{
公共画笔前景
{
获取{return(Brush)GetValue(TextBlock.ForegroundProperty);}
设置{SetValue(TextBlock.ForegroundProperty,value);}
}
公共布尔可见{get;set;}
公共字符串通道{get;set;}
公共重写字符串ToString()
{
返回string.Format(“{0}-{1}-{2}”,通道,前景,可见);
}
}
}
您不需要SolidColorBrush吗?我认为画笔没有颜色属性。第二个文本块的DataContext是什么?这个对象真的有一个叫做前景的属性吗?你可以很容易地用它来检查。您还应该检查调试输出是否有任何警告,失败的绑定通常会打印在输出中。@ColinPear-您认为使用SolidColorBrush可以工作吗?但是我没有收到任何错误?@dowhilefor-没有收到任何编译错误或警告。@ColinPear-尝试使用SolidColorBrush没有任何区别颜色仍然是绿色。@blindmeis-很抱歉回复太晚。它绑定到复选框。我更新了我的代码。但是它仍然不起作用。复选框i没有内容演示者-那么带前景绑定的datatemplate将永远不会发生?还是我错过了什么?
private readonly RangeObservableCollection<Info> _validInfo;
Info.Foreground = Brushes.Red;
_validInfo.Add(Info);
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); }
}
}
<Grid>
<Grid.Resources>
<DataTemplate DataType="{x:Type TestForeground:Info}">
<TextBlock Text="{Binding}" Foreground="{Binding Foreground}"/>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding MySource}">
</ListBox>
</Grid>
<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>
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);
}
}
}