2

xml代码:

<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); }
        }
    }

xml.cs:

private readonly RangeObservableCollection<Info> _validInfo;

Info.Foreground = Brushes.Red;                                        
_validInfo.Add(Info);

上面的代码没有改变文本块的前景色我做错了什么?

4

2 回答 2

1

我尝试了您的代码并为我工作。您可以发布您的数据模板生效的代码吗?我用一个列表框来做。

编辑

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); }
    }

}

xml

<Grid>
    <Grid.Resources>
        <DataTemplate DataType="{x:Type TestForeground:Info}">
            <TextBlock Text="{Binding}" Foreground="{Binding Foreground}"/>
        </DataTemplate>
    </Grid.Resources>
    <ListBox ItemsSource="{Binding MySource}">

    </ListBox>
</Grid>
于 2012-11-29T07:35:45.613 回答
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);
        }
    }
}
于 2012-12-07T21:45:48.580 回答