[英]Can't bind DataGridTextColumn property from another thread (WPF)
I'm updating DataGrid through binding from different thread (Task). 我正在通过从不同线程(任务)进行绑定来更新DataGrid。
I have zero problems with updating column values. 更新列值有零问题。
<DataGridTextColumn Header="NAME" MinWidth="100" Width="10*" Binding="{Binding name}" />
But some of my columns should also have different background color that is coming from database. 但是我的某些列也应该具有来自数据库的不同背景色。 And I can't update background property. 而且我无法更新背景属性。
<DataGridTextColumn Header="" Width="10">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding color}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
I'm running into this issue: 我遇到了这个问题:
Must create DependencySource on same Thread as the DependencyObject 必须在与DependencyObject相同的线程上创建DependencySource
Could you please advice what I do need to do? 您能建议我需要做什么吗?
PS Freeze() helps, but I suppose to think that it's not a way to do it. PS Freeze()会有所帮助,但我想这不是一种方法。
So I solved it by making converter. 所以我通过制作转换器解决了它。
I have made new class 我上了新课
public sealed class ColorCodeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
...
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
...
}
}
Then connect it as resouce in the App.xaml 然后将其连接为App.xaml中的资源
<local:ColorCodeConverter x:Key="ColorCodeConverter" />
And in the MainWindow.xaml I have following 在MainWindow.xaml中,我有以下内容
<DataGridTextColumn Header="" Width="10">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding C7, Converter={StaticResource ColorCodeConverter}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
So you just need to bind you string via ViewModel and that's all. 因此,您只需要通过ViewModel绑定字符串即可。 No need to use Freeze(), etc. 无需使用Freeze()等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.