简体   繁体   English

直接公开用户控件的子属性作为用户控件的属性

[英]Expose child properties of user control directly as property of user control

Without breaking MVVM, is there a way to expose some properties of a child control in a user control so that the window or other user control that utilizes it can access these properties directly? 在不破坏MVVM的情况下,是否有办法在用户控件中公开子控件的某些属性,以便使用它的窗口或其他用户控件可以直接访问这些属性?

For instance I have a user control that has a listview set up with gridviewcolumns, headers, and is bound to a view model. 例如,我有一个用户控件,该控件的listview设置有gridviewcolumns,标头,并绑定到视图模型。 But the list view in the user control has selected item properties and such that I'd like to expose to the host without having to do something like usercontrol.customListView.property. 但是用户控件中的列表视图具有选定的项目属性,因此我想向主机公开而不需要执行诸如usercontrol.customListView.property之类的操作。 Or is that how I should do it? 还是那应该怎么做? I'd like to go just usercontrol.property, omitting customListView. 我只想去usercontrol.property,省略customListView。 Perhap I should just create properties in the user controls code behind that return the list view controls properties that I want attached directly to the user control? 也许我应该在后面的用户控件代码中创建属性,该属性返回我想直接附加到用户控件的列表视图控件属性?

I feel like that latter option doesn't really break MVVM since they are exposed for the host to interact with, not really related to the view itself. 我觉得后一种选择并没有真正破坏MVVM,因为它们公开给主机与之交互,与视图本身并没有真正的关系。 Any suggested would be appreciated. 任何建议将不胜感激。

EDIT: In fact, I'd really like to have a SelectedItem property directly on the user control that is not ListViewItem or object, but actually of the datatype contained that doe like: 编辑:实际上,我真的很想直接在不是ListViewItem或对象的用户控件上拥有SelectedItem属性,但实际上包含的数据类型确实如下所示:

public MyDataType SelectedItem {
    get {
        return customListView.SelectedItem as MyDataType;
    }
}

Would that be permissible in MVVM? 在MVVM中允许吗? Because I don't see how I could have that in the ViewModel, seems like it would have to be in the partial class code behind. 因为我看不到如何在ViewModel中使用它,所以似乎必须在后面的部分类代码中。

This is pretty common task when you want to put something repeated into UserControl . 当您要将重复的内容放入UserControl时,这是非常常见的任务。 The simplest approach to do so is when you are not creating specialized ViewModel for that UserControl , but sort of making custom control (build with the use of UserControl for simplicity). 最简单的方法是不为该UserControl创建专门的ViewModel,而是进行自定义控件 (为简便起见,使用UserControl进行构建)。 End result may looks like this 最终结果可能看起来像这样

<UserControl x:Class="SomeNamespace.SomeUserControl" ...>
    ...
    <TextBlock Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}" ...>
</UserControl>

.

public partial class SomeUserControl : UserControl
{
    // simple dependency property to bind to
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(SomeUserControl), new PropertyMetadata());

    // has some complicated logic
    public double Value
    {
        get { return (double)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(double), typeof(SomeUserControl),
        new PropertyMetadata((d, a) => ((SomeUserControl)d).ValueChanged()));
    private void ValueChanged()
    {
        ... // do something complicated here
            // e.g. create complicated dynamic animation
    }

    ...
}

Usage will looks like this in containing window 用法在包含窗口中看起来像这样

<l:SomeUserControl Text="Text" Value="{Binding SomeValue}" ... />

As you can see SomeValue is bound to Value and there is no MVVM violations. 如您所见, SomeValue绑定到Value并且没有违反MVVM。

Of course, you can create a proper ViewModel if view logic is complicated or required too much bindings and it's rather easier to allow ViewModels to communicate directly (via properties/methods). 当然,如果视图逻辑复杂或需要太多绑定,并且允许ViewModels直接通信(通过属性/方法)比较容易,则可以创建适当的ViewModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM