使用 MVVM 的 WPF 只读依赖属性 [英] WPF ReadOnly Dependency Properties using MVVM

查看:28
本文介绍了使用 MVVM 的 WPF 只读依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近覆盖了 DevXpress WPF 网格,为自己提供了一个 SelectedObject 属性,我可以从松散绑定的 ViewModel 中访问该属性.

I've recently overridden the DevXpress WPF grid to give myself a SelectedObject property that I can access from my loosely bound ViewModel.

我已经创建了一个 SelectedObject 依赖属性,并将它绑定到我的 XAML 中的 OneWayToSource.

I've made a SelectedObject dependency property and have it bound OneWayToSource in my XAML.

一切正常,但如果我尝试将其设为只读(为了完整性),我会收到编译错误并说我无法绑定到只读属性.下面的代码可以编译,我已经包含(但重新删除了)我在尝试获取属性 ReadOnly 时一直在尝试的部分.

Everthing works fine, but if I try to make it ReadOnly (for completeness) I get a compile error and says I can't bind to a ReadOnly property. The code below compiles, I've included (but rem'd out) the bits I've been trying in my attempts to get the property ReadOnly.

有人可以帮忙吗?

我的重写控件的依赖属性如下所示:

The Dependency Property of my overridden control looks like:

  //public static readonly DependencyPropertyKey SelectedRowKey = DependencyProperty.RegisterReadOnly("SelectedObject", typeof(object), typeof(MyGrid), new PropertyMetadata(null));
//public static readonly DependencyProperty SelectedObjectProperty = SelectedRowKey.DependencyProperty;

public readonly static DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(MyGrid), new PropertyMetadata(null));

public object SelectedObject
{
    get
    {

        return GetValue(SelectedObjectProperty);
    }
    set
    {
        throw new NotImplementedException();
    }
}

XAML 是:

 <StackPanel>
  <devxgrid:MyGrid AutoPopulateColumns="True" DataSource="{Binding Animals}" SelectedObject="{Binding MyObject, Mode=OneWayToSource}" Width="300" Height="300">
    <devxgrid:MyGrid.View>
        <MyGrid:TableView AllowEditing="False" Name="GridView" AutoWidth="True" />
    </devxgrid:MyGrid.View>
 </devxgrid:MyGrid>
</StackPanel>

推荐答案

您正在尝试在 XAML 中设置 SelectedObject 属性.如果是只读的,如何设置?

You're trying to set the SelectedObject property in the XAML. If it's read-only, how can you set it?

对不起,我的错.刚刚意识到你想要做什么,你是对的,它应该起作用.但是,WPF 不支持这种情况,至少在 3.5 中是这样.

sorry, my bad. Just realized what you're trying to do, and you're right that it should work. However, WPF doesn't support this scenario, at least in 3.5.

编辑 2:刚刚签入 .NET 4 和相同的故事.

Edit 2: Just checked in .NET 4 and same story.

顺便说一下,如果您被其他人的只读 DP 困住了,而您正试图将其推送"到虚拟机中,您可以使用附加行为来解决此问题.例如,假设您希望 VM 知道视图的 ActualWidthActualHeight 属性.您可以编写一个 SizeWatcherBehavior 附加到 FrameworkElement 并侦听大小变化.检测到时,这些大小更改将推送到您的 VM 可以绑定到的读/写附加属性:

By the way, if you're stuck with someone else's readonly DP that you're trying to "push" into a VM, you can use an attached behavior to workaround this. For example, suppose you want your VM to be aware of the ActualWidth and ActualHeight properties of your view. You can write a SizeWatcherBehavior that attaches to the FrameworkElement and listens for size changes. When detected, those size changes are pushed to read/write attached properties that your VM can bind to:

<Grid local:SizeWatcherBehavior.Watch="True"
    local:SizeWatcherBehavior.Width="{Binding WidthOnVM, Mode=OneWayToSource}"
    local:SizeWatcherBehavior.Height="{Binding HeightOnVM, Mode=OneWayToSource}"/>

这篇关于使用 MVVM 的 WPF 只读依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆