2016-05-12 14 views
0

私は、ユーザーコントロールの中にあるlistBoxのイメージにVMプロパティ(オブザーバーコレクション)をコードで動的にバインドしようとしています。ウィンドウ、 が動作していません。WPFデータのバインディングViewModelプロパティのリストボックス内のユーザーコントロール内のコード

これは、ユーザーコントロールのXAMLです:

<WrapPanel x:Name="panel" HorizontalAlignment="Center" Focusable="False" FocusManager.IsFocusScope="False"> 
    <ListBox x:Name="MazeListBox" ItemsSource="{Binding}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid x:Name="MazeUniformGrid" Columns="{Binding VM_MazeCols}" Rows="{Binding VM_MazeRows}"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Image x:Name="Img" Margin="-6" Source="{Binding}" Focusable="False"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</WrapPanel> 

これは、外窓のXAMLでユーザーコントロールです:

<Controls:MazeBoard1 x:Name="you" Margin="0,0,650,0" Grid.Column="0" Height="Auto" Width="Auto" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
    <Controls:MazeBoard1 x:Name="other" Margin="650,0,0,0" Grid.Column="0" Height="Auto" Width="Auto" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center"/> 

これは私がのCSに動的にバインドする方法ですウィンドウ:

  Binding youBinding = new Binding 
     { 
      Path = new PropertyPath("VM_MazeDisplay"), 
     }; 

     Binding otherBinding = new Binding 
     { 
      Path = new PropertyPath("VM_OtherMazeDisplay"), 
     }; 
     you.MazeListBox.SetBinding(ContentControl.ContentProperty,youBinding); 
     other.MazeListBox.SetBinding(ContentControl.ContentProperty, otherBinding); 

ご協力いただきありがとうございます。 ありがとう

答えて

1

すべてはあなたのXAMLに奇妙に見える...しかし、あなたは、次のない場合: you.MazeListBox.SetBinding(ListBox.DataContextProperty, youBinding) または you.SetBinding(UserControl.DataContextProperty, youBinding) あるいはyou.MazeListBox.SetBinding(ListBox.ItemsSourceProperty, youBinding)(あなたがあなたのXAMLにバインドを削除する必要があります)。

期待どおりの結果が得られました。

ただし、DataContextの設定だけでなく、この時点でバインドを行うのはなぜですか? you.DataContext = VM_MazeDisplayのようなものです(VMのインスタンスがそのように呼ばれていると仮定します)。

さらに、ListBoxWrapPanelに入れるのはなぜですか?

+0

完璧に動作します、ありがとうございます!それらはすべて今後の計画だったが、その間に私は何を得たのか – NatanC

+0

OK!喜んで助けた:) –

関連する問題