在 WPF 中使用 MVVM 更新其他绑定的绑定

本文关键字:绑定 更新 其他 MVVM WPF | 更新日期: 2023-09-27 17:55:51

>有人对如何解决我的问题有任何建议吗?我有一个绑定到ModelCheckoutRecord对象集合的数据网格(ListModelCheckoutRecords)。我正在将所选项目绑定到ModelCheckoutRecordSelectedItemModelCheckoutRecord )。在所选项目的绑定中,我想更新另一个数据网格和网格拆分器 (ResponseVisibility ) 的可见性绑定。从其他绑定更新绑定的功能似乎不起作用?

我的 XAML 行如下所示:

            <Grid>
                <Grid.RowDefinitions>
                    <View:RowDefinitionExtended Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding ListModelCheckoutRecord}" SelectedItem="{Binding SelectedModelCheckoutRecord}" MouseLeftButtonDown="DataGrid_MouseLeftButtonDown" CommandManager.PreviewExecuted="DataGrid_DeletePreviewExecuted" IsReadOnly="False" CanUserAddRows="False" CanUserDeleteRows="True" Margin="15" Grid.Row="0"/>
                <GridSplitter HorizontalAlignment="Stretch" Visibility="{Binding ResponseVisibility}" Grid.Row="1"/>
                <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding ListModelResponseRecord}" SelectedItem="{Binding SelectedModelResponseRecord}" MouseLeftButtonDown="DataGrid_MouseLeftButtonDown" IsReadOnly="False" CanUserAddRows="False" CanUserDeleteRows="False" Visibility="{Binding ResponseVisibility}" Margin="15" Grid.Row="2"/>
            </Grid>

给我带来麻烦的精简代码如下:

    private Visibility responseVisibility = Visibility.Visible;
    public Visibility ResponseVisibility
    {
        get { return responseVisibility; }
        set
        {
            if (value == responseVisibility)
            {
                return;
            }
            responseVisibility = value;
            RaisePropertyChanged("ResponseVisibility");
        }
    }
    private List<ModelCheckoutRecord> listModelCheckoutRecord;
    public List<ModelCheckoutRecord> ListModelCheckoutRecord
    {
        get { return listModelCheckoutRecord; }
        set
        {
            if (value == listModelCheckoutRecord)
            {
                return;
            }
            listModelCheckoutRecord = value;
            RaisePropertyChanged("ListModelCheckoutRecord");
            ResponseVisibility = Visibility.Collapsed;
        }
    }
    private ModelCheckoutRecord selectedModelCheckoutRecord;
    public ModelCheckoutRecord SelectedModelCheckoutRecord
    {
        get { return selectedModelCheckoutRecord; }
        set
        {
            if (value == null)
            {
                ResponseVisibility = Visibility.Collapsed;
                return;
            }
            else
            {
                ResponseVisibility = Visibility.Visible;
            }
            if (selectedModelCheckoutRecord == value)
            {
                return;
            }
            RaisePropertyChanged("SelectedModelCheckoutRecord");
        }
    }

谢谢

理查

在 WPF 中使用 MVVM 更新其他绑定的绑定

假设您真的希望在仅在null SelectedItem的情况下具有折叠的可见性,那么实际上没有必要为此使用代码。以下 XAML 可以很好地完成这项工作。

只需为源 DataGrid 命名(在以下示例中,我假定 Grid 为名称),并在目标 DataGrid 中使用如下所示的样式:

<DataGrid x:Name="TargetGridJustToShowWhereThisGoes">
    <DataGrid.Style>
        <Style TargetType="DataGrid">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Grid, Path=SelectedItem}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Style>
</DataGrid>