none
DataGrid 绑定数据源后使用Converter给行加颜色不被触发,只有滚动DataGrid内容时候才触发Converter并刷新行色这是为什么? RRS feed

  • 问题

  • 绑定的SourceTable是时时变化的,在DataGrid 中也能看到绑定的内容在实时刷新,但是Converter只有在滚动DataGrid内容时才触发并刷新行色

    XAML:

        <UserControl.Resources>
            <ResourceDictionary>
                <local:ColorConvert x:Key="ColorConvert"/>
            </ResourceDictionary>
        </UserControl.Resources>
            <ExtendedGridControl:ExtendedDataGrid x:Name="grid" ItemsSource="{Binding SourceTable,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" HideColumnChooser="True" GroupByControlVisibility="Visible"
                              CanUserAddRows="False" OnlyTwoWaySorting="True" SearchAutoFilterVisibility="Visible" AutoFilterFontSize="12">
                <Controls:DataGrid.Columns>
                    <ExtendedColumn:ExtendedDataGridCheckBoxColumn Header="TextColumnName" Width="*" AllowAutoFilter="False" CanUserSort="True" IsReadOnly="True">
                        <ExtendedColumn:ExtendedDataGridCheckBoxColumn.ElementStyle>
                            <Style TargetType="Grid">
                                <Setter Property="Background" Value="{Binding Converter={StaticResource ColorConvert}}" />
                            </Style>
                            </ExtendedColumn:ExtendedDataGridCheckBoxColumn.ElementStyle>
                    </ExtendedColumn:ExtendedDataGridCheckBoxColumn>
                </Controls:DataGrid.Columns>
                <Controls:DataGrid.GroupStyle>
                  
                </Controls:DataGrid.GroupStyle>
            </ExtendedGridControl:ExtendedDataGrid>
    c# (Converter)
        [ValueConversion(typeof(string), typeof(string))]
        public class ColorConvert : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                People strValue = value as People;
                if (strValue != null && strValue.name == "peter")
                {
                    return "Red";
                }
                return null;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return "";
            }
        }
        public class People
        {
            public int age { get; set; }
            public string name { get; set; }
        }

    2014年2月21日 4:10

答案

  • 你好,

    从你的XAML代码看来,使用了一个扩展的DataGrid控件,以我的经验,是这个控件的问题。依据你的代码,我用WPF DataGrid控件实现了你的需求:

    XAML:

    <Window.Resources>
            <ResourceDictionary>
                <local:ColorConvert x:Key="ColorConvert"/>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <DataGrid x:Name="grid" ItemsSource="{Binding SourceTable,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False"
                              CanUserAddRows="False" >
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Header="TextColumnName" Width="*" CanUserSort="True" IsReadOnly="True">
                        <DataGridCheckBoxColumn.ElementStyle>
                            <Style TargetType="CheckBox">
                                <Setter Property="Background" Value="{Binding Converter={StaticResource ColorConvert}}" />
                            </Style>
                        </DataGridCheckBoxColumn.ElementStyle>
                    </DataGridCheckBoxColumn>
                </DataGrid.Columns>
                <DataGrid.GroupStyle>
    
                </DataGrid.GroupStyle>
            </DataGrid>
            <Button Grid.Row="1" Content="Refresh" Click="Button_Click" />
        </Grid>

    Code Behind:

    public partial class MainWindow : Window
    {
            Foo a = new Foo();
            ObservableCollection<People> t = new ObservableCollection<People>();
    
            public MainWindow()
            {
                InitializeComponent();
                
                t.Add(new People() {age=10,name="peter" });
                t.Add(new People() { age = 11, name = "jack" });
                t.Add(new People() { age = 13, name = "franklin" });
                a.SourceTable = t;
                grid.DataContext = a;
            }
    
            private void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                t.Add(new People() { age = 20, name = "peter" });
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                //  DispatcherTimer setup
                System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
                dispatcherTimer.Start();
            }
    }
    
    public class Foo
    {
            private ObservableCollection<People> _person;
            public ObservableCollection<People> SourceTable
            {
                get { return _person; }
                set
                {
                    _person = value;
                }
            }
    }
    ......

    点击Button 后,可见新行中的转换没有问题,所以如果你一定要使用此三方控件,可能需要去其官网上报问题,或者使用默认的DataGrid实现你的需求。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 MaJiXiang 2014年2月24日 2:44
    2014年2月24日 2:25
    版主

全部回复

  • 你好,

    从你的XAML代码看来,使用了一个扩展的DataGrid控件,以我的经验,是这个控件的问题。依据你的代码,我用WPF DataGrid控件实现了你的需求:

    XAML:

    <Window.Resources>
            <ResourceDictionary>
                <local:ColorConvert x:Key="ColorConvert"/>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="30" />
            </Grid.RowDefinitions>
            <DataGrid x:Name="grid" ItemsSource="{Binding SourceTable,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False"
                              CanUserAddRows="False" >
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Header="TextColumnName" Width="*" CanUserSort="True" IsReadOnly="True">
                        <DataGridCheckBoxColumn.ElementStyle>
                            <Style TargetType="CheckBox">
                                <Setter Property="Background" Value="{Binding Converter={StaticResource ColorConvert}}" />
                            </Style>
                        </DataGridCheckBoxColumn.ElementStyle>
                    </DataGridCheckBoxColumn>
                </DataGrid.Columns>
                <DataGrid.GroupStyle>
    
                </DataGrid.GroupStyle>
            </DataGrid>
            <Button Grid.Row="1" Content="Refresh" Click="Button_Click" />
        </Grid>

    Code Behind:

    public partial class MainWindow : Window
    {
            Foo a = new Foo();
            ObservableCollection<People> t = new ObservableCollection<People>();
    
            public MainWindow()
            {
                InitializeComponent();
                
                t.Add(new People() {age=10,name="peter" });
                t.Add(new People() { age = 11, name = "jack" });
                t.Add(new People() { age = 13, name = "franklin" });
                a.SourceTable = t;
                grid.DataContext = a;
            }
    
            private void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                t.Add(new People() { age = 20, name = "peter" });
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                //  DispatcherTimer setup
                System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
                dispatcherTimer.Start();
            }
    }
    
    public class Foo
    {
            private ObservableCollection<People> _person;
            public ObservableCollection<People> SourceTable
            {
                get { return _person; }
                set
                {
                    _person = value;
                }
            }
    }
    ......

    点击Button 后,可见新行中的转换没有问题,所以如果你一定要使用此三方控件,可能需要去其官网上报问题,或者使用默认的DataGrid实现你的需求。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 MaJiXiang 2014年2月24日 2:44
    2014年2月24日 2:25
    版主
  • 非常感谢您的帮助,我现在是通过定时刷新DataGrid控件的方式临时解决了这个问题。
    2014年2月24日 2:44