【问题标题】:Refresh style in datagrid bound to datatable WPF刷新绑定到数据表 WPF 的数据网格中的样式
【发布时间】:2010-12-09 21:56:48
【问题描述】:

我有一个DataGrid 绑定到一个DataTable 并且DataGrid 有一个单元格样式,它根据其值(正黑或负红)更改单元格中数字的Foreground。 当DataTable 更新时,DataGrid 会正确更新,因此绑定工作正常。 问题是Style 只是在第一次加载DataGrid 时应用。当 DataGrid 被绑定更新时,如果负数变为正数,Foreground 将保持红色而不是变为黑色。

我错过了什么,任何财产或事件吗?

提前致谢。

【问题讨论】:

  • 如果您提供一些重现问题的示例代码,我们会更容易帮助您解答
  • 是的...一些代码肯定会有所帮助

标签: wpf datagrid datatable refresh styles


【解决方案1】:

我不确定你是如何尝试这样做的。我尝试过的任何方法都可以正常工作。检查此代码并找出问题所在

Xaml:

 <StackPanel Loaded="StackPanel_Loaded" >
    <StackPanel.Resources>
        <WpfApplication50:ValueToForegroundColorConverter x:Key="valueToForegroundColorConverter"/>
        <DataTemplate x:Key="Valuetemplate">
            <TextBlock x:Name="txt" Text="{Binding Value}" Foreground="{Binding Path=Value,Converter={StaticResource valueToForegroundColorConverter}}"/>
        </DataTemplate>
    </StackPanel.Resources>
    <dtgrd:DataGrid ItemsSource="{Binding Source}"  
                     Name="datagrid"                          
                     ColumnHeaderHeight="25" 
                     AutoGenerateColumns="False"   
                          >
        <dtgrd:DataGrid.Columns>
            <dtgrd:DataGridTemplateColumn CellTemplate="{StaticResource Valuetemplate}" Header="Value"/>
        </dtgrd:DataGrid.Columns>
    </dtgrd:DataGrid>
    <Button Height="30" Click="Button_Click"/>
</StackPanel>

在你的代码隐藏中

 public partial class Window10 : Window,INotifyPropertyChanged
{  
    private DataTable source;
    public DataTable Source
    {
        get { return source; }
        set { source = value; OnPropertyChanged("Source"); }
    }
    public Window10()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if(PropertyChanged!=null)
        PropertyChanged(this,new PropertyChangedEventArgs(name));
    }

    #endregion

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Source.Rows.Add("-1");
    }

    private void StackPanel_Loaded(object sender, RoutedEventArgs e)
    {
        Source = new DataTable();
        Source.Columns.Add("Value");
        Source.Rows.Add("1");
    }
}

也是这个转换器

 class ValueToForegroundColorConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        SolidColorBrush brush = new SolidColorBrush(Colors.Black);
        int val = 0;
        int.TryParse(value.ToString(), out val);
        if (val < 0)
            brush = new SolidColorBrush(Colors.Red);
        return brush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2011-01-31
    • 2017-05-14
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2012-05-23
    • 2012-11-21
    • 2011-08-14
    相关资源
    最近更新 更多