【问题标题】:WPF Master\Detail: How to keep Master from updating when Detail is edited?WPF Master\Detail:编辑详细信息时如何防止大师更新?
【发布时间】:2014-08-14 12:12:21
【问题描述】:

我有一个非常直接的 MVVM Master\Detail 窗口 -

XAML sn-p:

        <Grid Grid.Row="0" Grid.Column="0" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Label Content="ID:" Grid.Row="0" Grid.Column="0"/>
            <Label Content="{Binding SelectedCustomer.CustId}" Grid.Row="0" Grid.Column="1"/>

            <Label Content="Name:" Grid.Row="1" Grid.Column="0"/>
            <TextBox Text="{Binding SelectedCustomer.Name}"  Grid.Row="1" Grid.Column="1"/>

            <Label Content="Address:" Grid.Row="2" Grid.Column="0"/>
            <TextBox Text="{Binding SelectedCustomer.Address}"  Grid.Row="2" Grid.Column="1"/>
            <Label Content="City:" Grid.Row="3" Grid.Column="0"/>
            <TextBox Text="{Binding SelectedCustomer.City}"  Grid.Row="3" Grid.Column="1"/>
            <Label Content="State:" Grid.Row="4" Grid.Column="0"/>
            <TextBox Text="{Binding SelectedCustomer.State}"  Grid.Row="4" Grid.Column="1"/>
            <Label Content="ZIP:" Grid.Row="5" Grid.Column="0"/>
            <TextBox Text="{Binding SelectedCustomer.ZIP}"  Grid.Row="5" Grid.Column="1"/>

        </Grid>

        <Grid Grid.Row="1" Grid.Column="0">
            <DataGrid ItemsSource="{Binding CustomerCollection}" SelectedItem="{Binding SelectedCustomer}"></DataGrid>
        </Grid>
    </Grid>

模型中没有什么花哨的东西:

    public ObservableCollection<Customer> CustomerCollection { get; set; }

    private Customer _selectedCustomer;
    public Customer SelectedCustomer
    {
        get
        {
            return _selectedCustomer;
        }
        set
        {
            _selectedCustomer = value;
            OnPropertyChanged("SelectedCustomer");
        }
    }

我可以选择一个主行,详细信息将适当填写。这部分效果很好。

当我编辑详细信息时,我的问题出现了,它在用户点击保存之前更改了主文件。如果我更改其中一个属性并且它失去焦点,则数据绑定会将主行设置为与新信息相同。我尝试了各种版本的 Mode=OneWay,但都没有帮助。

如何使 SelectedItem 的数据绑定只从数据网格中移出,而不是返回?当用户单击保存时,我将刷新数据网格,这不是问题。对我来说,记录不要在中途更改比过于频繁地刷新网格更重要。

【问题讨论】:

    标签: c# wpf mvvm datagrid


    【解决方案1】:

    您必须在集合的属性上设置 OneTime 绑定,而不是集合本身。 尝试禁用网格上的 autogeneratecolumns 并使用一次性绑定自己定义列。这应该会得到你需要的行为。

    类似这样的东西(未测试):

    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Customers}" SelectedItem="
         {Binding SelectedCustomer}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name, Mode=OneTime}" />
            <DataGridTextColumn Binding="{Binding Address, Mode=OneTime}" />
        </DataGrid.Columns>
    </DataGrid>
    

    如果您确实想将细节与主对象分开,我建议您使用单独的 selectedCustomer 属性,并在完成编辑后将该属性复制到绑定到网格的集合中。一次性绑定很好,但您必须手动更新网格,如果您想使用严格的 MVVM,这需要一些代码。另请注意,当您编辑详细信息时,SelectedCustomer 仍会在 ViewModel 中的集合上更新,网格还不会反映更改。因此,如果您点击保存,最新信息仍会写入数据库。

    【讨论】:

    • 我将其标记为答案,因为它有一点帮助,而且它提供了真正的答案:分离功能以便可以重复使用。
    【解决方案2】:

    您可以使用UpdateSourceTrigger 但在这种情况下,您必须以详细形式运行所有控件并手动更新源代码。

    更复杂的解决方案是您可以再次从服务器而不是从网格将选定的行加载到详细表单中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2019-08-23
      • 1970-01-01
      • 2019-07-17
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多