【问题标题】:Converter not firing after collection update集合更新后转换器未触发
【发布时间】:2017-10-31 03:19:38
【问题描述】:

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

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

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;

【问题讨论】:

  • 请向我们展示您认为应该更新的内容。代码,我的意思是。您“更新绑定集合”的代码。
  • 嗨@EdPlunkett 我已经添加了您要求的部分。
  • 顺便说一句,您的大写约定区域有点迷失方向。 LineItem 像属性一样大写,但它是本地的。

标签: c# wpf styles propertychanged converters


【解决方案1】:

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

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

您在转换器中关心的是Measurement.DeltaMeasurement.Tol。当其中任何一个发生更改时,绑定应更新其目标。你不想以聪明的方式做到这一点。你只想要一个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();
    }
}

【讨论】:

  • 感谢您的解决方案和解释!
猜你喜欢
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
相关资源
最近更新 更多