[英]WPF: Reusing control with slightly different call on ViewModel?
我有一个控件ActionRequiredControl.xaml,其中包含一个带有枚举列表的组合框。 此控件具有ActionRequiredViewModel作为其DataContext,并且以下调用用于填充组合框:
public IEnumerable<ActionType> Actions
{
get
{
return Enum.GetValues(typeof(ActionType)).Cast<ActionType>();
}
}
并且组合框如下。
<StackPanel Orientation="Vertical">
<Label>Action:</Label>
<ComboBox Width="170" MinHeight="45" Margin="5,0,5,5" Padding="5"
SelectedItem="{Binding Action, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Actions, Mode=OneTime}"
VerticalContentAlignment="Center" IsEnabled="{Binding IsUsed}"/>
</StackPanel>
当前在两个地方使用了此控件(除了组合框以外,还有更多其他用途)-但是,我想在第三个地方重用它,只有“动作”函数返回的不同列表。
最好的方法是什么? 我唯一想到的就是创建一个从ActionRequiredViewModel继承并覆盖Actions方法的新类。
最简单的解决方案是我在问题中指出的解决方案-我创建了一个继承自ActionRequiredViewModel的新类,并覆盖了“ Actions”方法以返回不同的枚举列表。
是的,创建其他ViewModel是最简单的解决方案。 不久前我处于类似的情况,我的第一个实现(使用MVVM light)是使用两个VM,并在ViewModelLocator中注册这两个VM,并为每个VM提供一个属性:
SimpleIoc.Default.Register<ViewModel1>();
SimpleIoc.Default.Register<ViewModel2>();
public ViewModel ViewModel1Instance
{
get
{
return ServiceLocator.Current.GetInstance<ViewModel1>();
}
}
public ViewModel ViewModel2Instance
{
get
{
return ServiceLocator.Current.GetInstance<ViewModel2>();
}
}
然后在XAML中:
<local:MyUserControl DataContext="{Binding ViewModel1Instance,
Source={StaticResource Locator}}" ...
但是,为更加灵活起见,我稍后将其替换为另一个解决方案,该解决方案提供了更好的自定义。
我满足了您的要求,并提出了以下解决方案:
在用户控件后面的代码中创建依赖项属性作为代理:
public static readonly DependencyProperty ComboBoxItemsProperty =
DependencyProperty.Register("ComboBoxItems",
typeof(IEnumerable<object>), typeof(TestUserControl));
public IEnumerable<object> ComboBoxItems
{
get
{
return (IEnumerable<object>)GetValue(ComboBoxItemsProperty);
}
set
{
SetValue(ComboBoxItemsProperty, value);
}
}
在用户控件的XAML中:
...ItemsSource="{Binding ComboBoxItems, Mode=OneWay, RelativeSource=
{RelativeSource AncestorType={x:Type UserControl}}}"...
在主视图模型中:
public IEnumerable<object> Items
{
get
{
return Enum.GetValues(typeof(ActionType)).Cast<object>();
}
}
在主要的XAML中:
<local:TestUserControl ComboBoxItems="{Binding DataContext.Items,
Mode=OneTime,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
</local:TestUserControl>
这对我有用,但是使用object
而不是自定义类型并不是最优雅的解决方案。
PS:这是我关于SO的第一个答案,(而且我还没有问题……)。
由于我多年来一直将SO用作提示和技巧的主要来源,所以我认为我可以尝试回馈一些东西...
当然,我承认:尝试回答并不是学习新主题的最糟糕方法(而且WPF对我而言还很陌生,所以请不要怪我回答不佳:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.