【问题标题】:Changing property in MultiBinding doesn't affect the actual object在 MultiBinding 中更改属性不会影响实际对象
【发布时间】:2015-08-16 10:49:16
【问题描述】:

我有以下场景:

<Setter Property="ItemsSource">
    <Setter.Value>
        <MultiBinding Converter="{StaticResource itemsSourceConv}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
            <Binding Path="DataSource" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"></Binding>
            <Binding Path="DataSet" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay"></Binding>
        </MultiBinding>
    </Setter.Value>
</Setter>

我在绑定到某些数据的控件上有一个依赖属性。 在控件的 XAML 中,我有这段代码,它必须从 DependancyProperty 获取数据并将其传递给控件的视图模型。

我已经确定使用 Multi Binding 会更容易。

问题是在我执行转换器后,控件 ViewModel 中的 DataSet 属性没有设置。

这是 MultiBinding 转换器的外观

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    var dataFromWindow = values[0] as List<object>;
    var controlViewModel = values[1];
    controlViewModel = dataFromWindow ; //After execution this object is changed but the object that values[1] references in the viewmodel isn't changed.
    return dataFromWindow;

}

MultiBinding 是否会创建我传递给它的对象的副本,但不能使用对它的引用?

编辑:

通过在转换器内部直接分配 DataContext.DataSet 来解决。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    它确实适用于参考。但就是这样。您重新分配您的本地参考。而已。您的本地参考是原始参考副本的副本。

    您将需要传递父级,然后设置 DataSet 属性或调用 DataSet 的方法来进行您想要的更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2019-01-25
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      相关资源
      最近更新 更多