c# - 集合更新后转换器未触发

标签 c# wpf styles propertychanged converters

我遇到了转换器的问题......一旦绑定(bind)集合更新,它们就不会触发,尽管它们在集合首次填充时触发。每当集合发生变化时,我想让它们开火。

到目前为止,我已经构建了一个简单的转换器:

public class TableConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        VM.Measurement t = ((VM.Measurement)((TextBlock)value).DataContext);
        if (t.Delta != null)
        {
            if (Math.Abs((double)t.Delta) < t.Tol)
                return "Green";
            else
                return "Red";
        }
        else
            return "Red";
    }

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

链接到样式

<conv:TableConverter x:Key="styleConvStr"/>

<Style x:Key="CellStyleSelectorTol" TargetType="syncfusion:GridCell">
    <Setter Property="Background" Value="{Binding   RelativeSource={RelativeSource Self}, Path=Content, Converter={StaticResource styleConvStr}}" />
</Style>

在这个DataGrid中用到了

 <syncfusion:SfDataGrid x:Name="CheckGrid" BorderBrush="White" Grid.Row="1" Grid.Column="1" AllowEditing="True"  ItemsSource="{Binding ChecksList, Mode=TwoWay}"  Background="White"   SnapsToDevicePixels="False"
                            ColumnSizer="None"  AllowResizingColumns="False" AllowTriStateSorting="True" AllowDraggingColumns="False" CurrentCellEndEdit="CheckGrid_CurrentCellEndEdit" AutoGenerateColumns="False"
                            NavigationMode="Cell" HeaderRowHeight="30" RowHeight="21"   GridPasteOption="None" Margin="20 10 10 10" AllowGrouping="True" SelectedItem="{Binding SelectedLine, Mode=TwoWay}"
                           SelectionUnit="Row"  SelectionMode="Single" RowSelectionBrush="#CBACCB"  VirtualizingPanel.IsVirtualizing="True"  Visibility="Visible">

                                <syncfusion:GridTextColumn Width="100" ColumnSizer="SizeToCells" AllowEditing="True"  MappingName="Measured" CellStyle="{StaticResource CellStyleSelectorTol}" HeaderText="Measured" TextAlignment="Center"   AllowFiltering="False" FilterBehavior="StringTyped"/>

VM 包含一个 Observable 集合,它实现 NotifyPropertyChanged 一直到测量类。这些属性很好地启动,所以它不是一个有约束力的问题。

 private ObservableCollection<Measurement> _checkList = new ObservableCollection<Measurement>();
    public ObservableCollection<Measurement> ChecksList
    {
        get
        {
            return _checkList;
        }
        set
        {
            _checkList = value;
            NotifyPropertyChanged();
        }
    }

如有任何帮助,我们将不胜感激。

谢谢

编辑: 这是更新集合的代码。很抱歉它很乱。 Lineitem 是为其更新 Measured 和 Delta 的选定行。一旦修改,这些就会正确显示在网格中。

public void NewMeasurement(VM.Measurement measurementShell)
{
    using (VMEntity DB = new VMEntity())
    {
        var Check = CheckSets.Where(x => x.ID == SelectedLine.ID).First();
        if (Check.Measurement == null)
        {
            Check.Measurement = measurementShell.Index;
            var Lineitem = ChecksList.Where(x => x.ID == SelectedLine.ID).First();
            var measurement = DB.Measurements.Where(x => x.Index == Check.Measurement).First();
            Lineitem.Measured = (double)measurement.measurement1;
            Lineitem.Delta = Lineitem.Measured - Lineitem.Target;

最佳答案

好的,看起来问题在于您正在更改单元格内容项(LineItem,在 NewMeasurement() 中) 的属性方法),但它仍然是同一个对象,因此单元格的内容不会改变。单元格的 Content 是绑定(bind)的来源。如果那没有改变,绑定(bind)将不会唤醒并更新目标。您正在引发 PropertyChanged,但此特定绑定(bind)无法知道您希望它监听this 对象以了解那些 属性更改。足够简单的修复:我们将开始准确地告诉它要听什么。

幸运的是,解决方案意味着简化您的一些代码。将 UI 控件传递到值转换器很奇特,也没有必要。

您在转换器中关心的是Measurement.DeltaMeasurement.Tol。当任何一个改变时,绑定(bind)应该更新它的目标。你不想以一种聪明的方式做到这一点。您只需要为每个绑定(bind)一个 Binding。这是 Binding 的工作。

所以告诉 Binding 你关心那些属性,并重写转换器以接受它们作为参数。

<Style x:Key="CellStyleSelectorTol" TargetType="syncfusion:GridCell">
    <Setter 
        Property="Background" 
        >
        <Setter.Value>
            <MultiBinding Converter="{StaticResource styleConvStr}">
                <Binding Path="Delta" />
                <Binding Path="Tol" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

转换器:

public class TableConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        //  I'm inferring that Measurement.Delta is Nullable<double>; if that's 
        //  not the case, change accordingly. Is it Object instead? 
        double? delta = (double?)values[0];
        double tol = (double)values[1];

        if (delta.HasValue && Math.Abs(delta.Value) < tol)
        {
            return "Green";
        }
        return "Red";
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

关于c# - 集合更新后转换器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44265798/

相关文章:

c# - 有没有标准的方法来访问 .Net 中的 Windows 服务文件?

c# - 无法通过上下文(Entity Framework Core)将 unicode(表情符号)写入 SQL Server 表

wpf - Telerik、Infragistics、XCeed、

c# - 如何在WPF中 move 按钮

html - CSS 拆分边框颜色

c# - using 语句可以用大括号代替吗?

c# - NAudio 从耳机录音

c# - 如何将 WPF CollectionViewGroup 类型更改为自定义类型并在 ListCollectionView 中使用它

css - 如何使样式为 none 的查询中的按钮在 JSP 中使用 session 样式 block ?

Javascript - React Native - 如何将 TextInput 和 Text 放在同一行中