2016-10-11 5 views
-1

私はNine Men's Morrisボードゲームを作成し、MVVMを初めて利用しようとしています。ゲームでは、ゲームの開始時に各プレーヤーの残っているゲームピースを表示し、各プレーヤーの正しい色でGameBoardViewModelにそれらを設定したいと思います。次に、ボードにピースが追加されるたびにObservableCollectionをそれに応じて更新します。ViewModelsのObservableCollectionを作成し、コードからDataTemplate値をオーバーライドする

問題はObservableCollectionを実装するときにゲームボードのビューモデルから各ゲームの色プロパティを正しく設定できないことです。私は私のViewModel用の私のDataTemplateは、viewmodelのコードで設定された値をオーバーライドし、色は決して決してないと思います。

Colorプロパティは、私のGamePieceのViewModelで定義され、INotifyPropertyChangedインターフェイスを実装しています。 DataTemplateから設定されるSizeプロパティは、正常に動作します。

私gamepieceは、基本的には、楕円形のように見えるようにオーバーライドし、デフォルトのスタイルを持つチェックボックスである:私のGameBoardViewModelで

  d:DesignHeight="300" d:DesignWidth="300" Margin="-1000000" Width="{Binding Size}" Height="{Binding Size}"> 
<UserControl.Resources> 
    <Style TargetType="CheckBox"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type CheckBox}"> 
        <Grid> 
         <Ellipse Stroke="Black" Fill="{Binding Path=Color, Mode=TwoWay}" x:Name="OuterEllipse"> 
          <Ellipse.BitmapEffect> 
           <BevelBitmapEffect BevelWidth="8" EdgeProfile="BulgedUp" Smoothness="0.745"/> 
          </Ellipse.BitmapEffect> 
         </Ellipse> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 
<Grid Name="root"> 
    <CheckBox > 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Click"> 
       <i:InvokeCommandAction Command="{Binding ClickPieceCmd}" CommandParameter="{Binding ActionName}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </CheckBox> 


</Grid> 

私が持っている:

private void CreateHeaderGamePieces() 
    { 
     PlayersPieces = new ObservableCollection<GamePieceViewModel>(); 
     for (int pieceCount = 0; pieceCount < _maxPieceCount; pieceCount++) 
     { 
      GamePieceViewModel gamePiece = new GamePieceViewModel(); 
      gamePiece.Color = "Black"; 
      if (pieceCount > 9) 
      { 
       gamePiece.Color = "Red"; 
      } 

      PlayersPieces.Add(gamePiece); 
     } 
    } 

私のGameBoardResources.xamlには私が持っています:私が持っている私のGameBoardView.xamlで

<DataTemplate DataType="{x:Type vm:GamePieceViewModel}"> 
    <vw:GamePieceView Size="15"/> 
</DataTemplate> 

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="90" 
ItemsSource="{Binding PlayersPieces}" DockPanel.Dock="Left"> 
      <ListView.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid Columns="9"/> 
       </ItemsPanelTemplate> 
      </ListView.ItemsPanel> 
     </ListView> 

これは、それがのObservableCollectionを移入した後、今どのように見えるかです:私は定義することができますどのように ListView of gamepieces

ObservableCollectionにデータを埋め込むとき、私のGameBoardViewModelから各ピースの色?

+1

を 'GamePieceView'色プロパティのいくつかの種類を持つべきである(例えば'Background')を使用して、ビューモデルのColorプロパティにバインドされます。ビューモデルのColorプロパティの型として 'string'の代わりに' Brush'を使用することができます。 – Clemens

+0

そのSizeプロパティは、GamePieceViewのDependencyPropertyであり、GamePieceのviewModelの対応するプロパティにバインドされています。しかし、GameBoardViewModelのコードからそれを設定しようとしても、それは機能しません。"Size"プロパティを機能させるためには、DataTemplateで定義する必要があります。 – Makezu

+1

GamePieceViewはDataContextを明示的に設定するため、ListViewItemからDataContextを継承することができなくなります。 – Clemens

答えて

0

1.GamePieceViewModel:

public class GamePieceViewModel : BindableBase 
{ 
    private Brush _BackgroundBrush; 
    public Brush BackgroundBrush 
    { 
     get 
     { 
      return _BackgroundBrush; 
     } 
     set 
     { 
      _BackgroundBrush = value; 
      OnPropertyChanged("StatusBackgroundBrush"); 
     } 
    } 
    } 

2.GamePieceView:

<UserControl x:Class="WpfApplication1.GamePieceView" 
     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:wpfApplication1="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" d:DataContext="{d:DesignInstance wpfApplication1:GamePieceViewModel}"> 
<Canvas Background="LightGray"> 
    <Ellipse 
    Fill="{Binding BackgroundBrush}" 
    Height="15" 
    Width="15"/> 
</Canvas> 

3.MainViewModel:

public class MainViewModel : BindableBase 
{ 
    public ObservableCollection<GamePieceViewModel> PlayersPieces { get; set; } 

    public MainViewModel() 
    { 
     PlayersPieces = new ObservableCollection<GamePieceViewModel>(); 
     for (int pieceCount = 0; pieceCount < 18; pieceCount++) 
     { 
      GamePieceViewModel gamePiece = new GamePieceViewModel(); 
      gamePiece.BackgroundBrush = Brushes.Black; 
      if (pieceCount > 9) 
      { 
       gamePiece.BackgroundBrush = Brushes.Red; 
      } 

      PlayersPieces.Add(gamePiece); 
     } 
    } 
} 

4.MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:customControlLib="clr-namespace:CustomControlLib;assembly=CustomControlLib" 
    xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
    mc:Ignorable="d" 
    Title="MainWindow" 
    DataContext="{StaticResource MainViewModel}"> 
<Window.Resources> 

</Window.Resources> 
<Grid > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
    </Grid.RowDefinitions> 
    <ListView Grid.Row="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="90" 
       ItemsSource="{Binding PlayersPieces}" DockPanel.Dock="Left"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate DataType="{x:Type wpfApplication1:GamePieceViewModel}"> 
       <wpfApplication1:GamePieceView/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="9"/> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 
    </ListView> 
</Grid> 

+0

あなたの提案をお寄せいただきありがとうございますが、違いはありません。色はまだリストビューのデフォルトのオレンジ色のままです。この実装の違いは、私の現在のものではなく、より洗練されたGamePieceView(古いものと同じように働くことはできませんでした)と、Colorの代わりにBrushesを使用することです。 – Makezu

+0

なぜあなたは良い結果を得ていないのか分かりません。自分でチェックしました。新しいプロジェクトを開き、私のコードをすべてコピーしてください。期待通りに動作します。次に調整してください。 – Rom

+0

そうですね。それは新鮮なソリューションに取り組んだ。私は完全なアプリケーションで作業することができませんでした。おそらく私はビューのコンストラクタで私のdatacontextを初期化する必要があります – Makezu

関連する問題