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

Silverlight MVVM Light-从xaml设置控件依赖项属性值,silverlight,xaml,data-binding,mvvm-light,Silverlight,Xaml,Data Binding,Mvvm Light,我在SL4上使用MVVM灯。我的视图正在通过定位器解析视图模型,一切正常 我的问题是,我的一个视图有一个属性,我需要在另一个视图中设置该属性 i、 e.HomeView可以有多个组件视图实例。但是在主视图上,我想在组件视图上设置属性。我曾尝试向视图的代码隐藏中添加依赖项属性,然后可以从HomeView中设置该属性,但我的组件视图模型没有选择它 这可能吗 ComponentControl.cs public enum CustomStyle { Active, Draft,

我在SL4上使用MVVM灯。我的视图正在通过定位器解析视图模型,一切正常

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

i、 e.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);
}
public CustomStyle CustomType
{
    get { return _customType; }
    set
    {
        if (value == _customType)
            return;

        _customType = value;
        base.RaisePropertyChanged("CustomType");
    }
}
private CustomStyle _customType;
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); }
}
组件视图模型.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);
}
public CustomStyle CustomType
{
    get { return _customType; }
    set
    {
        if (value == _customType)
            return;

        _customType = value;
        base.RaisePropertyChanged("CustomType");
    }
}
private CustomStyle _customType;
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.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);
}
public CustomStyle CustomType
{
    get { return _customType; }
    set
    {
        if (value == _customType)
            return;

        _customType = value;
        base.RaisePropertyChanged("CustomType");
    }
}
private CustomStyle _customType;
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); }
}
组件视图.xaml

<Grid>
    <common:ComponentControl 
            DataContext="{Binding Path=WorkflowList, Mode=OneWay}" 
            CustomType="{Binding Path=CustomType, Mode=TwoWay, 
                                 ElementName=root}" />
</Grid>
<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>

HomeView.xaml

<Grid>
    <common:ComponentControl 
            DataContext="{Binding Path=WorkflowList, Mode=OneWay}" 
            CustomType="{Binding Path=CustomType, Mode=TwoWay, 
                                 ElementName=root}" />
</Grid>
<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>

我想我可以帮你一点忙,刚才我回答了一个类似的问题:

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