【发布时间】: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