【问题标题】:Update bindings from other bindings using MVVM in WPF在 WPF 中使用 MVVM 从其他绑定更新绑定
【发布时间】:2015-06-12 13:35:21
【问题描述】:

有人对如何解决我的问题有任何建议吗?我有一个绑定到ModelCheckoutRecord 对象(ListModelCheckoutRecords)的集合的数据网格。我将所选项目绑定到ModelCheckoutRecord (SelectedItemModelCheckoutRecord)。在所选项目的绑定中,我想更新另一个数据网格和网格拆分器 (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");
        }
    }

谢谢

理查德

【问题讨论】:

  • 在您的SelectedModelCheckoutRecord 中,您根据所选项目是否为空来设置ResponseVisibility。在这种情况下,它可能永远不会为空,因此可见性将始终可见。在这里重新思考你的逻辑。

标签: c# wpf xaml mvvm binding


【解决方案1】:

假设您真的希望仅在 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>

【讨论】:

  • 感谢 Maverik,代码工作正常。我之前在其他场景中遇到过这个问题 - 尝试从其他绑定中更改不同的元素绑定 - 以供将来参考,这可能吗?
  • 只要你能想到触发器值(只有相等有效)和一个依赖属性来设置触发器,你就可以使用上面的模式。
猜你喜欢
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 2013-04-25
  • 2014-07-12
  • 2019-01-24
  • 2021-03-01
相关资源
最近更新 更多