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>
类:
public class Info : DependencyObject
{
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
}
xaml.cs:
private readonly RangeObservableCollection<Info> _validInfo;
Info.Foreground = Brushes.Red;
_validInfo.Add(Info);
上面的代码没有改变文本块的前景颜色我做错了什么?
答案 0 :(得分:1)
我尝试了你的代码并为我工作。你可以发布你的datatemplate生效的代码吗?我用列表框来做。
修改
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
<Grid>
<Grid.Resources>
<DataTemplate DataType="{x:Type TestForeground:Info}">
<TextBlock Text="{Binding}" Foreground="{Binding Foreground}"/>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding MySource}">
</ListBox>
</Grid>
答案 1 :(得分:0)
首先,对于任何未来的问题,我一定会提供一个“完整”的例子 - 你粘贴的代码需要稍微修改才能运行。
那就是说,那里有一些拼写错误可能会导致问题 - 以下略有清理的代码会正确绑定:
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>
代码:
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);
}
}
}