具有显式接口的依赖属性

发布于 2024-07-10 23:40:21 字数 505 浏览 8 评论 0原文

我正在为我们的部门制作一个 WPF 演示,向他们展示 WPF 的优势,同时尝试遵守我们的开发标准(依赖项注入和将对象开发为显式接口)。

我现在已经有点墙了。 我正在使用 MVVM 设计模式实现视图,每次更新视图模型 (VM) 上的属性时,我都需要更新 TextBlocks Text 属性。 为此,我将 VM 属性定义为依赖属性,并将视图中的 TextBlocks Text 属性绑定到它。

现在,MV 属性位于我的界面上,并且(根据我们的开发标准)已明确定义。 从视图中,我将视图中 TextBlock 的 Text 属性绑定到 Dependency Properties 属性(不是静态部分),但这不会在依赖属性值更改时更新我的​​视图(我知道如何绑定到显式接口,因此这据我所知这不是问题)。

任何帮助将不胜感激。 我可以将依赖属性与显式接口一起使用吗? 如果我可以,如果不能,你对我在这种情况下能做什么有什么想法吗?

感谢您的阅读,期待您的回复。

亚当

I am knocking together a WPF demo for our department at work to show them the advantages of WPF whilst trying to adhere to our development standards (dependency injection and developing objects to an explicit interface).

I have come to a bit of a wall now. I am implementing the View using the MVVM design pattern and I need to update a TextBlocks Text property every time the property on the View Model (VM) is updated. For this I would define the VM property as a Dependency Property and bind the TextBlocks Text property in the View to it.

Now the MV property is on my interface and is (as per our development standards) explicitly defined. From the View I bind the Text property of the TextBlock in the View to the Dependency Properties property (not the static part) but this does not update my View when the dependency properties value changes (I know how to bind to an explicit interface so this is not the problem as far as I can see).

Any help would really be appreciated. Can I use Dependency Properties with Explicit Interfaces? If I can how, if not have you got any ideas on what I can do in this situation?

Thank you for reading and I look forward to your responses.

Adam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹梦到西洲 2024-07-17 23:40:21

我不完全确定我是否正确理解了您的问题,但为什么不在您的 ViewModel 上简单地使用 INotifyPropertyChanged 呢?

例如:

interface MyInterface : INotifyPropertyChanged
{
    string Text { get; set; }
}

class MyViewModel : MyInterface
{
    private string text;
    public string Text 
    {
        get { return text; }
        set 
        { 
            if (text != value)
            {
               text = value;
               // TODO: Raise the NotifyPropertyChanged event here
            }
        }
    }
}

有了这个,您应该能够简单地绑定到 Text 属性

I'm not entirely sure if I understood your question right, but why not simply use INotifyPropertyChanged on your ViewModel?

for example:

interface MyInterface : INotifyPropertyChanged
{
    string Text { get; set; }
}

class MyViewModel : MyInterface
{
    private string text;
    public string Text 
    {
        get { return text; }
        set 
        { 
            if (text != value)
            {
               text = value;
               // TODO: Raise the NotifyPropertyChanged event here
            }
        }
    }
}

With this you should be able to simply bind to the Text property

厌倦 2024-07-17 23:40:21

这个问题有点令人困惑,但我会尝试一下。 我尝试了依赖属性的几种变体,但无法使其与以下接口一起使用。

interface IViewModel
{
    string Text { get; set; }
}

我使用以下语法在实现类上注册了该属性(每个语法都在不同的测试中)。

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
        typeof(string), typeof(IViewModel));

然后我尝试显式或隐式实现该接口,但没有成功。 唯一有效的组合是我使用时。

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
        typeof(string), typeof(ImplementingClass));

如果您遇到问题并且正在寻找其他 WPF 示例,您可能需要查看一下。

祝你好运。

The question is mildly confusing but I will take a stab at it. I tried several variations of the dependency property and could not make it work with the following interface.

interface IViewModel
{
    string Text { get; set; }
}

I registered the property on the implementing class using the following syntax (each one in a different test).

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
        typeof(string), typeof(IViewModel));

Then I tried implementing the interface explicitly or implicitly to no avail. The only combination that did work is when I used.

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
        typeof(string), typeof(ImplementingClass));

If you run into problems and are looking for other WPF samples you may want to check out.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文