Silverlight MVVM Light - 从 xaml 设置控件依赖属性值

发布于 2024-10-18 20:32:43 字数 2633 浏览 1 评论 0原文

我正在使用 MVVM Light 和 SL4。我的视图正在通过定位器解析其视图模型,一切都很好。

我的问题是我的一个视图有一个属性,我需要在另一个视图中设置。

即一个 HomeView 可以有多个组件视图实例。但在该主视图上,我想在组件视图上设置属性。我尝试将依赖属性添加到视图后面的代码中 - 然后我可以从 HomeView 设置该依赖属性,但我的组件视图模型没有选择它。

这可以吗?

ComponentControl.cs

public enum CustomStyle
{
    Active,
    Draft,
    Completed
}

public class ComponentControl : Control
{
    public ComponentControl()
    {
        DefaultStyleKey = typeof (ComponentControl);
    }

    public CustomStyle CustomType
    {
        get { return (CustomStyle)GetValue(CustomTypeProperty); }
        set { SetValue(CustomTypeProperty, value); }
    }

    public static readonly DependencyProperty CustomTypeProperty =
        DependencyProperty.Register("CustomType",
        typeof(CustomStyle),
        typeof(ComponentControl), null);
}

ComponentViewModel.cs

public CustomStyle CustomType
{
    get { return _customType; }
    set
    {
        if (value == _customType)
            return;

        _customType = value;
        base.RaisePropertyChanged("CustomType");
    }
}
private CustomStyle _customType;

ComponentView.xaml.cs

public static readonly DependencyProperty CustomTypeProperty =
    DependencyProperty.Register("CustomType", 
    typeof(CustomStyle), 
    typeof(ComponentView), null);

public CustomStyle CustomType
{
    get { return (CustomStyle)GetValue(CustomTypeProperty); }
    set { SetValue(CustomTypeProperty, value); }
}

ComponentView.xaml

<Grid>
    <common:ComponentControl 
            DataContext="{Binding Path=WorkflowList, Mode=OneWay}" 
            CustomType="{Binding Path=CustomType, Mode=TwoWay, 
                                 ElementName=root}" />
</Grid>

HomeView.xaml强>

<Grid x:Name="LayoutRoot">
    <common:HomeControl x:Name="homeControl">
        <common:HomeControl.ActiveContent>
            <local:ComponentView x:Name="active" CustomType="Active" />
        </common:HomeControl.ActiveContent>
        <common:HomeControl.DraftContent>
            <local:ComponentView x:Name="draft" CustomType="Draft" />
        </common:HomeControl.DraftContent>
        <common:HomeControl.CompletedContent>
            <local:ComponentView x:Name="completed" CustomType="Completed" />
        </common:HomeControl.CompletedContent>
    </common:HomeControl>
</Grid>

I am using MVVM Light with SL4. My View's are resolving their View Model via the locator, all is fine.

My problem is that one of my views has a property that I need to set in another view.

i.e. a HomeView can have many instances of a component view. But on that home view I want to set the property on the component view. I've tried adding a dependency property to the code behind of the view - which I can then set from HomeView, but my component view model does not pick it up.

Is this possible to do?

ComponentControl.cs

public enum CustomStyle
{
    Active,
    Draft,
    Completed
}

public class ComponentControl : Control
{
    public ComponentControl()
    {
        DefaultStyleKey = typeof (ComponentControl);
    }

    public CustomStyle CustomType
    {
        get { return (CustomStyle)GetValue(CustomTypeProperty); }
        set { SetValue(CustomTypeProperty, value); }
    }

    public static readonly DependencyProperty CustomTypeProperty =
        DependencyProperty.Register("CustomType",
        typeof(CustomStyle),
        typeof(ComponentControl), null);
}

ComponentViewModel.cs

public CustomStyle CustomType
{
    get { return _customType; }
    set
    {
        if (value == _customType)
            return;

        _customType = value;
        base.RaisePropertyChanged("CustomType");
    }
}
private CustomStyle _customType;

ComponentView.xaml.cs

public static readonly DependencyProperty CustomTypeProperty =
    DependencyProperty.Register("CustomType", 
    typeof(CustomStyle), 
    typeof(ComponentView), null);

public CustomStyle CustomType
{
    get { return (CustomStyle)GetValue(CustomTypeProperty); }
    set { SetValue(CustomTypeProperty, value); }
}

ComponentView.xaml

<Grid>
    <common:ComponentControl 
            DataContext="{Binding Path=WorkflowList, Mode=OneWay}" 
            CustomType="{Binding Path=CustomType, Mode=TwoWay, 
                                 ElementName=root}" />
</Grid>

HomeView.xaml

<Grid x:Name="LayoutRoot">
    <common:HomeControl x:Name="homeControl">
        <common:HomeControl.ActiveContent>
            <local:ComponentView x:Name="active" CustomType="Active" />
        </common:HomeControl.ActiveContent>
        <common:HomeControl.DraftContent>
            <local:ComponentView x:Name="draft" CustomType="Draft" />
        </common:HomeControl.DraftContent>
        <common:HomeControl.CompletedContent>
            <local:ComponentView x:Name="completed" CustomType="Completed" />
        </common:HomeControl.CompletedContent>
    </common:HomeControl>
</Grid>

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

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

发布评论

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

评论(1

唔猫 2024-10-25 20:32:43

我想我可以帮助你一点,不久前我回答了一个类似的问题:

Silverlight:如何绑定到父视图的 DataContext?

我的示例中的子视图包含一个依赖属性,该属性的值在父视图中设置。子视图使用 ElementName="this" 的绑定绑定到该依赖属性

I think I can help you a bit, a while ago I answered a similar question :

Silverlight: How to bind to parent view's DataContext?

The child view in my example contains a dependency property, which has it's value set in the parent view. The child view binds to that dependency property using a binding with ElementName="this"

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