【问题标题】:WPF Combobox Binding broken after Collection repopulation [duplicate]集合重新填充后 WPF 组合框绑定损坏 [重复]
【发布时间】:2018-04-19 08:42:39
【问题描述】:

我有以下情况。 我有一个 ListView 设置如下:

<ListView Margin="3,3,3,3" ItemsSource="{Binding Toner}" SelectedValue="{Binding CurrentToner}" IsSynchronizedWithCurrentItem="True">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="BorderBrush" Value="LightGray" />
                <Setter Property="BorderThickness" Value="0,0,0,1" />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>

                    </Grid.ColumnDefinitions>
                    <Label Grid.Row="0" Grid.Column="0" Content="{Binding Id}" FontWeight="Bold"></Label>
                    <Label Grid.Row="0" Grid.Column="1" Content="{Binding Name}" FontWeight="Bold"></Label>
                    <Label Grid.Row="1" Grid.Column="0" Content="{Binding Ratio}">
                    </Label>
                    <Label Grid.Row="1" Grid.Column="1" Content="{Binding TonerType.Name}"  ></Label>

                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我在对应的ViewModel中设置了CurrentToner属性:

private Toner currentToner = new Toner();
public Toner CurrentToner
    {
        get => currentToner;
        set
        {
            SetProperty(ref currentToner, value);
            updateTonerCommand.RaiseCanExecuteChanged();
            deleteTonerCommand.RaiseCanExecuteChanged();
            increaseAmountCommand.RaiseCanExecuteChanged();
            RaisePropertyChanged(nameof(Changes));
        }

    }

现在在与上面相同的视图中,我也有一些控件绑定到 CurrentToner 的属性。

<TextBox Style="{StaticResource TextBoxStyle}" Grid.Row="0" Grid.Column="1" Text="{Binding CurrentToner.Name}"></TextBox>
        <TextBox Style="{StaticResource TextBoxStyle}" Grid.Row="1" Grid.Column="1" Text="{Binding CurrentToner.Level}" ></TextBox>
        <ComboBox  Grid.Row="2" Grid.Column="1" SelectedValue="{Binding CurrentToner.TonerType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding TonerTypes, Mode=OneWay}" DisplayMemberPath="Name"></ComboBox>
        <TextBox Style="{StaticResource TextBoxStyle}" Grid.Column="1" Grid.Row="3" Text="{Binding CurrentToner.Note}" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"></TextBox>

现在我可以在 ListView 中单击 arround,我的 TextControls 将按预期更新。 但是,如果我重新加载 Observable Collection 中的数据,则所选项目将不再在 Combobox 中更新(数据仍然可用,我使用 Debugger 进行了检查,Combobox 仍然可以在 CurrentToner 中设置 TonerType) 我使用实体框架从 SQL Server 重新加载数据:

private void ReloadToner()
    {
        Toner.Clear();
        using (var uw = new UnitOfWork(new Gritly_DEVEntities1()))
        {
            Toner.AddRange(uw.Toners.GetAll());

        }

    }

如何实现我的重新加载逻辑以再次更新所选项目? (它在重新加载之前工作)

编辑:它应该如何以及如何不是的屏幕截图。 it should be like this not like this

EDIT2:我找到了答案,我必须重写 TonerType 上的 Equals() 以根据 Id 而不是 Adress 进行比较

【问题讨论】:

    标签: wpf entity-framework data-binding prism-6


    【解决方案1】:

    如果您重新加载数据,那么您的集合中有新对象。
    如果它在那里,我错过了那个对象类型是什么,但无论如何我们称它为 Toner。 确保碳粉具有唯一标识它的东西,例如 ID。 如果您查看 Toner 对象,您可能可以确定哪个是旧的当前对象,但计算机非常具有字面意义。他们需要您确切地告诉他们,他们应该如何知道碳粉是一种特定的碳粉,而不是任何旧的碳粉。

    当您要重新读取数据时,将该 ID 保存在私有变量中。 读取数据。
    构建您的可观察集合。 在 id 上使用一些 linq 匹配找到适当的条目。
    比如:

    Toner newSelectedToner = Toners.Where(x=>x.ID = lastSelectedID).FirstOrDefault();
    

    顺便说一句,这只是空气代码。 将 CurrentToner 设置为该实例。
    此外,在您的 Toner 上覆盖 equals 以使用 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2013-06-05
      • 2013-05-13
      • 2021-07-23
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多