Silverlight MVVM Light - 从 xaml 设置控件依赖属性值
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我可以帮助你一点,不久前我回答了一个类似的问题:
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"