0

我正在使用 MVVM 开发 WPF (C#) 应用程序。我创建了一个新项目,它是一个只关注我遇到的问题的简化。

在视图中有一个由 PanelButton 组成的 Panel,该 PanelButton 由两个按钮和 PanelDisplay 组成。

这个想法是,当按下橙色按钮时,PanelDisplay 应将其颜色更改为橙​​色,而当按下绿色按钮时,PanelDisplay 应更改为绿色。

面板代码:

<UserControl x:Class="WpfApplication1.View.Panel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:view="clr-namespace:WpfApplication1.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.3*"/>
            <ColumnDefinition Width="0.7*"/>
        </Grid.ColumnDefinitions>
                    
        <view:PanelButtons Grid.Column="0"></view:PanelButtons>
        <view:PanelDisplay Grid.Column="1"></view:PanelDisplay>
                        
    </Grid>
</UserControl>

PanelButtons.xaml 的代码:

<UserControl x:Class="WpfApplication1.View.PanelButtons"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
    <viewModel:PanelButtonsAndDisplayVM x:Key="panelButtonsAndDisplayVM"/>        
</UserControl.Resources>

<Grid Background="LightGray">
    <StackPanel>
        <Button Width="64" Height="64"
                Command="{Binding Source={StaticResource panelButtonsAndDisplayVM}, Path=PressedOrange}">Orange</Button>
        <Button Width="64" Height="64"
                Command="{Binding Source={StaticResource panelButtonsAndDisplayVM}, Path=PressedGreen}">Green</Button>
    </StackPanel>
        
</Grid>

PanelDisplay.xaml 的代码:

<UserControl x:Class="WpfApplication1.View.PanelDisplay"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
    <viewModel:PanelButtonsAndDisplayVM x:Key="panelButtonsAndDisplayVM"/>
</UserControl.Resources>

<Grid Background="{Binding Source={StaticResource panelButtonsAndDisplayVM},Path=Color}" >
        
</Grid>

问题是 PanelDisplay 没有改变它的颜色,为了解决这个问题,我创建了一个单例类来启动一个事件并将 PanelDisplay 订阅到该事件并且它工作,但我需要在 MainWindow 中有两个“面板”,所以如果我使用这个解决方案这两个面板将改变它们的颜色,因为它们都将获得相同的事件,并且应该只更新一个 PanelDisplay。

主窗口代码:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:view="clr-namespace:WpfApplication1.View"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.5*"/>
        <RowDefinition Height="0.5*"/>
    </Grid.RowDefinitions>
    
    <view:Panel Grid.Row="0"/>
    <view:Panel Grid.Row="1"/>
</Grid>

那么,如何分别实现每个 PanelDisplay 呢?有任何想法吗?

4

2 回答 2

1

您应该为每个UserControl. 例如,您可以将 a then 作为 a和 a 的PanelViewModel属性。PanelDisplayViewModelPanelButtonsViewModel

  1. 按钮被按下PanelButtonsViewModel
  2. 该方法调用 toPanelViewModel以报告事件。
  3. 然后面板通过调用PanelDisplayViewModel.

在这里避免视图模型的静态资源,只DataContext在每个用户控件上使用标准,它应该可以工作。

于 2012-12-17T18:40:30.857 回答
1

您的 PanelButtons.xaml 和 PanelDisplay.xaml 不使用 PanelButtonsAndDisplayVM 类的相同实例,因为它们都在资源中声明自己的实例。

相反,将 Panel.xaml 中的 PanelButtonsAndDisplayVM 实例声明为 DataContext,以便将其传播到所有后代控件:

<UserControl x:Class="WpfApplication1.View.Panel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:view="clr-namespace:WpfApplication1.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="600">
    <UserControl.DataContext>
        <view:PanelButtonsAndDisplayVM/>
    </UserControl.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.3*"/>
            <ColumnDefinition Width="0.7*"/>
        </Grid.ColumnDefinitions>
        <view:PanelButtons Grid.Column="0"></view:PanelButtons>
        <view:PanelDisplay Grid.Column="1"></view:PanelDisplay>
    </Grid>
</UserControl>

并像这样在 PanelButtons.xaml 中使用它:

<UserControl x:Class="WpfApplication1.View.PanelButtons"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid Background="LightGray">
        <StackPanel>
            <Button Width="64" Height="64"
                Command="{Binding PressedOrange}">Orange</Button>
            <Button Width="64" Height="64"
                Command="{Binding PressedGreen}">Green</Button>
        </StackPanel>
    </Grid>
</UserControl>

在 PanelDisplay.xaml 中是这样的:

<UserControl x:Class="WpfApplication1.View.PanelDisplay"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid Background="{Binding Path=Color}">
    </Grid>
</UserControl>
于 2012-12-17T19:23:36.180 回答