XAML 从单独的线程更新绑定值时,未触发WPF转换器

qeeaahzv  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(100)

我有一个包含DataGrid的WPF应用程序,它被绑定到C# DataTable上。它有一个转换器,可以根据单元格内的值更改单元格的背景颜色。在正常使用下,一切都按预期工作,但当我从单独的线程更新DataTable的值时,显示的值发生了变化,但转换器没有被触发。
DataGrid:

<UserControl.Resources>
    <utility:CellColorConverter x:Key="ColorConverter" />

    <Style x:Key="BlueDataGridStyle" TargetType="{x:Type DataGrid}">
        <Setter Property="CellStyle" Value="{DynamicResource BlueDataGridCellStyle}" />
    </Style>

    <Style x:Key="BlueDataGridCellStyle" TargetType="DataGridCell" >
        <Setter Property="Background">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ColorConverter}">
                    <MultiBinding.Bindings>
                        <Binding RelativeSource="{RelativeSource Self}" />
                        <Binding Path="Row" />
                    </MultiBinding.Bindings>
                </MultiBinding>
            </Setter.Value>
        </Setter>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<DataGrid
    Name="dgSimple"
    AutoGenerateColumns="True"
    ItemsSource="{Binding Grid}"
    IsReadOnly="True"
    CanUserResizeColumns="False"
    CanUserResizeRows="False"
    CanUserSortColumns="False"
    CanUserReorderColumns="False"
    Margin="6"
    SelectedIndex="{Binding SelectedRow}"
    CurrentColumn="{Binding SelectedColumn}"
    Style="{StaticResource BlueDataGridStyle}"
    HeadersVisibility="All"
    RowHeaderWidth="32"
    ColumnWidth="32">
</DataGrid>

转换器:

public class CellColorConverter : IMultiValueConverter
{
    object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values[0] is DataGridCell cell && values[1] is DataRow row)
        {
            try
            {
                string columnName = (string)cell.Column.Header;
                int columnIndex = cell.Column.DisplayIndex;

                if (columnIndex == 0)
                {
                    cell.FontStyle = FontStyles.Italic;
                    cell.FontWeight = FontWeights.Bold;
                    cell.FontSize = 20;
                    return new SolidColorBrush(Colors.Yellow);
                }

                string content = row.Field<string>(columnName); // Header must be same as column name

                if (content == "w")
                {
                    return new SolidColorBrush(Colors.Blue);
                }

                if (content == "h")
                {
                    return new SolidColorBrush(Colors.Red);
                }

                if (content == "m")
                {
                    return new SolidColorBrush(Colors.LightGray);
                }

                if (content == "c" || content == "b" || content == "s" || content == "d" || content == "p")
                {
                    return new SolidColorBrush(Colors.Gray);
                }

            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return new SolidColorBrush(Colors.Black); // Error! An Exception was thrown
            }
        }
        return new SolidColorBrush(Colors.Green); // Something else.
    }

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

我尝试调用NotifyPropertyChanged事件,希望这会触发转换器,但没有成功。

zour9fqk

zour9fqk1#

我假设问题是您没有使用Dispatcher调用setter。我没有看到你在另一个线程下触发的代码,但我会添加这样的代码:

//code fired on non UI thread.
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            //call code will trigger PropertyChanged event
        }));

相关问题