C#用户控制(MVVM)在MV中表现出属性

发布于 2025-02-06 04:34:26 字数 1407 浏览 1 评论 0原文

我觉得我可能在这里错过了某些人,或者它不可行(我很难相信)。

我有一个使用MVVM ArchTitecture的USERCONTROL。

Usercontrol看起来像这样。

 public partial class UserControl1 : UserControl
    {
        private string _labelContents;

        public UserControl1()
        {
            InitializeComponent();
            DataContext = new UserControl1_VM();
        }
    }

VM看起来像这样。

公共类UserControl1_vm:InotifyPropertychanged { 私有字符串_labelContents =“从VM设置”;

    public string LabelContent
    {
        get => _labelContents;
        set { _labelContents = value; OnPropertyChanged("LabelContent"); }
    }

    public event PropertyChangedEventHandler? PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

我希望能够放置一个主视图或类似的内容:

  <mv:UserControl1 LabelContent="My Text"></mv:UserControl1>

但VS指出,它“无法依次依据符号标记”。在视图模型中,这是可以理解的。如果不将该属性放入USERCONTROL中的代码,而是通过它传递该物业似乎是不可能的。我可能只是在寻找错误的事情。

这是一个非常基本的外观,但是我认为标签需要是依赖性属性,因为它与自我束缚在一起。

<mv:UserControl1 LabelContent="{Binding LabelText}"></mv:UserControl1>

对此的任何帮助都会很棒,因为它让我挠我的头,让我秃头!

只是为了让您知道它是否不是依赖属性,但似乎很笨拙。

欢呼

詹姆斯

I feel though i may be missing somethig here, or its not doable (which i find hard to belive).

I have a UserControl thats using MVVM archtitecture.

The UserControl looks like this.

 public partial class UserControl1 : UserControl
    {
        private string _labelContents;

        public UserControl1()
        {
            InitializeComponent();
            DataContext = new UserControl1_VM();
        }
    }

The VM looks like this.

public class UserControl1_VM : INotifyPropertyChanged
{
private string _labelContents = "Set from VM";

    public string LabelContent
    {
        get => _labelContents;
        set { _labelContents = value; OnPropertyChanged("LabelContent"); }
    }

    public event PropertyChangedEventHandler? PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

I want to be able to put in a MainView or similar this:

  <mv:UserControl1 LabelContent="My Text"></mv:UserControl1>

But VS states that it "Cannot relsolve symbol LabelContent". Which is understandable as its in the view model. Without putting that Property in to the code behind in the UserControl, and passing the value through it seems impossible. I may just be looking for the wrong thing.

This is a very basic exaple, but LabelContent i think needs to be a dependancy properties because it is bound to it self i.e ulimately.

<mv:UserControl1 LabelContent="{Binding LabelText}"></mv:UserControl1>

Any help with this would be great, as it has me scratching my head, and making me bald!!

Just to let you know if its not a Dependancy Property this works, but seems very clumsy.

Cheers

James

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

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

发布评论

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

评论(1

傲鸠 2025-02-13 04:34:26

您不需要userControl的任何视图模型,而只需要一个依赖性属性:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public string LabelContent
    {
        get { return (string)GetValue(LabelContentProperty); }
        set { SetValue(LabelContentProperty, value); }
    }

    public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register
    (
        nameof(LabelContent),
        typeof(string),
        typeof(UserControl1),
        new PropertyMetadata(String.Empty)
    );    
}

然后将其放在视图中并分配或绑定属性:

<mv:UserControl1 Name="uc1" LabelContent="My Text"/>

<mv:UserControl1 Name="uc2" LabelContent="{Binding Path=LabelContent, ElementName=uc1}"/>

you don't need any view models for UserControl, just a dependency property:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public string LabelContent
    {
        get { return (string)GetValue(LabelContentProperty); }
        set { SetValue(LabelContentProperty, value); }
    }

    public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register
    (
        nameof(LabelContent),
        typeof(string),
        typeof(UserControl1),
        new PropertyMetadata(String.Empty)
    );    
}

then put it in a view and assign or bind property:

<mv:UserControl1 Name="uc1" LabelContent="My Text"/>

<mv:UserControl1 Name="uc2" LabelContent="{Binding Path=LabelContent, ElementName=uc1}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文