0

MVVM を使用して WPF (C#) アプリケーションを開発しています。私が抱えている問題のみに焦点を当てた単純化された新しいプロジェクトを作成しました。

ビューには、2 つのボタンと PanelDisplay で構成される PanelButton によって構成される Panel があります。

オレンジ色のボタンが押されると 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 に 2 つの「パネル」が必要なので、このソリューションを使用する場合2 つのパネルは両方とも同じイベントを取得し、1 つの PanelDisplay のみを更新する必要があるため、色が変わります。

MainWindow のコード:

<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および aPanelViewModelのプロパティとして then を持つことができます。PanelDisplayViewModelPanelButtonsViewModel

  1. でボタンが押されますPanelButtonsViewModel
  2. PanelViewModelそのメソッドは、イベントを報告するために を呼び出します。
  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 に答える