【问题标题】:Binding textblock foreground color not working绑定文本块前景色不起作用
【发布时间】:2012-11-28 16:25:01
【问题描述】:

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

xaml.cs:

private readonly RangeObservableCollection<Info> _validInfo;

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

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

【问题讨论】:

  • 您不需要 SolidColorBrush 吗?我不认为画笔有颜色属性。
  • 第二个TextBlock的DataContext是什么?这个对象真的有一个叫做 Foreground 的属性吗?您可以使用snoop 轻松检查。您还应该检查调试输出是否有任何警告,失败的绑定通常会打印在输出中。
  • @ColinPear - 你认为使用 SolidColorBrush 会起作用吗?我没有收到任何错误?
  • @dowhilefor - 没有收到任何编译错误或警告。
  • @ColinPear - 尝试使用 SolidColorBrush 没有任何区别颜色仍然是绿色。

标签: c# xaml


【解决方案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); }
    }

}

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>

【讨论】:

  • @blindmeis - 抱歉回复晚了。它绑定到复选框。我更新了我的代码。但它仍然无法正常工作。
  • 没有内容演示者的复选框 - 所以与前台绑定的数据模板永远不会发生?还是我错过了什么?
【解决方案2】:

首先,对于任何未来的问题,我一定会提供一个“完整”的示例 - 您粘贴的代码甚至需要进行一些修改才能运行。

也就是说,其中有许多拼写错误可能会导致问题 - 以下稍微清理过的代码可以正确绑定:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2014-03-13
    • 2012-12-11
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多