简体   繁体   English

WPF:在ViewModel上使用稍微不同的调用来重用控件吗?

[英]WPF: Reusing control with slightly different call on ViewModel?

I have a control, ActionRequiredControl.xaml that includes a combo-box with a list of enums. 我有一个控件ActionRequiredControl.xaml,其中包含一个带有枚举列表的组合框。 This control has ActionRequiredViewModel as it's DataContext, and the following call is used to populate the combobox: 此控件具有ActionRequiredViewModel作为其DataContext,并且以下调用用于填充组合框:

    public IEnumerable<ActionType> Actions
    {
        get
        {
            return Enum.GetValues(typeof(ActionType)).Cast<ActionType>();
        }
    }

And the combo-box is as follows. 并且组合框如下。

   <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>

This control is used in two places currently (There is more to it than just the combobox) - however, I would like to re-use it in a third place, only have the "Actions" function return a different list of . 当前在两个地方使用了此控件(除了组合框以外,还有更多其他用途)-但是,我想在第三个地方重用它,只有“动作”函数返回的不同列表。

What's the best way to go about this? 最好的方法是什么? The only thing I can think of is creating a new class that inherits from ActionRequiredViewModel and overrides the Actions method. 我唯一想到的就是创建一个从ActionRequiredViewModel继承并覆盖Actions方法的新类。

最简单的解决方案是我在问题中指出的解决方案-我创建了一个继承自ActionRequiredViewModel的新类,并覆盖了“ Actions”方法以返回不同的枚举列表。

Yes, creating an additional ViewModel will be the simplest solution. 是的,创建其他ViewModel是最简单的解决方案。 I was in a similar situation some time ago, my first implementation (using MVVM light) was using two VMs, and registering both in the ViewModelLocator and providing a property for each: 不久前我处于类似的情况,我的第一个实现(使用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>();
    }
}

And then in XAML: 然后在XAML中:

<local:MyUserControl DataContext="{Binding ViewModel1Instance, 
    Source={StaticResource Locator}}" ...

However, to be a bit more flexible, I replaced this with another solution later, which provided better customization. 但是,为更加灵活起见,我稍后将其替换为另一个解决方案,该解决方案提供了更好的自定义。

I played a bit with your requirements and came to this solution: 我满足了您的要求,并提出了以下解决方案:

Creating a dependency property in the code behind of the user control as a proxy: 在用户控件后面的代码中创建依赖项属性作为代理:

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);
    }
}

In the XAML of the user control: 在用户控件的XAML中:

...ItemsSource="{Binding ComboBoxItems, Mode=OneWay, RelativeSource=
    {RelativeSource AncestorType={x:Type UserControl}}}"...

In the main view model: 在主视图模型中:

public IEnumerable<object> Items
{
    get
    {
        return Enum.GetValues(typeof(ActionType)).Cast<object>();
    }
 }

In the main XAML: 在主要的XAML中:

<local:TestUserControl ComboBoxItems="{Binding DataContext.Items, 
    Mode=OneTime, 
    RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
</local:TestUserControl>

This worked for me, but using object instead of the custom type is not the most elegant solution. 这对我有用,但是使用object而不是自定义类型并不是最优雅的解决方案。

PS: This is my very first answer on SO, (and no questions yet from me...). PS:这是我关于SO的第一个答案,(而且我还没有问题……)。

Since I used SO for many years as my primary source of tips and tricks, I thought I can TRY to give something back... 由于我多年来一直将SO用作提示和技巧的主要来源,所以我认为我可以尝试回馈一些东西...

And of course, I admit: trying to answer is not the worst method of learning a new topic (and WPF is fairly new to me, so please don´t blame me for a suboptimal answer :-) 当然,我承认:尝试回答并不是学习新主题的最糟糕方法(而且WPF对我而言还很陌生,所以请不要怪我回答不佳:-)

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

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