2011-02-03 74 views
3

我有兩個單獨的綁定問題,帶有包含texbox的itemtemplate的列表框。在列表框中綁定項目模板問題

1)一個listbox綁定到一個字符串列表。如何在創建的文本框內顯示每個字符串,並同時允許雙向綁定?沒有指定Path或XPath,不允許雙向綁定。

<ListBox Height="231" HorizontalAlignment="Left" Margin="0,167,0,0" Name="listBoxKeys" VerticalAlignment="Top" Width="219" ItemsSource="{Binding Path=SelectedPlatform.Keys}" SelectedItem="{Binding Path=SelectedKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 
     <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
        <TextBox Text="{Binding Mode=OneWay}" Margin="0,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
</ListBox> 

而且2)我使用另一個綁定到一個自定義KeyValuePair類的通用列表的列表框。 itemtemplate包含一個文本框和組合框。文本框文本綁定到每個KeyValuePair對象的key屬性,並將combobox selecteditem綁定到value屬性。我的問題是,我想組合充滿我的viewmodel中聲明的字符串列表,這些列表將在運行時更改。窗口的datacontext是聲明列表的視圖模型。我不知道我需要用來綁定組合框itemssource的確切語法。這裏是我的代碼:

<ListBox Height="393" HorizontalAlignment="Left" Margin="0,72,0,0" Name="listBoxActions" VerticalAlignment="Top" Width="254" ItemsSource="{Binding Path=SelectedPlayer.ControlProfile.MappedActions}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
      <TextBox Text="{Binding Key, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
      <ComboBox Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center" ItemsSource="{Binding ?}" SelectedItem="{Binding Value, Mode=TwoWay}"/> 
     </StackPanel> 
     </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 
+1

這確實應該是兩個獨立的問題,你會發現有些人能回答一個,而不是其他,所以你可能會得到兩個人回答正確。這會使答案難以標記爲正確。 – 2011-02-03 12:33:52

回答

4

的問題是雙向的源綁定本身不能工作,因爲這將意味着整個對象(字符串),爲其創建數據模板,時必須更換用戶更改文本框中的文本。顯然,這是行不通的。雙向綁定僅適用於綁定對象的可寫屬性。

在你的情況我建議建立在列表框中的項目(基本上是你的字符串視圖模型)視圖模型和數據模板就可以暴露出Value屬性,並綁定到它:

<ListBox Height="231" HorizontalAlignment="Left" Margin="0,167,0,0" 
     Name="listBoxKeys" VerticalAlignment="Top" Width="219" 
     ItemsSource="{Binding Path=SelectedPlatform.KeyViewModels}" 
     SelectedItem="{Binding Path=SelectedKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
        <TextBox Text="{Binding Value, Mode=TwoWay}" Margin="0,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
        </StackPanel> 
       </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 
+0

謝謝你。我創建了一個名爲ControllerKey的無用類,它有一個字符串屬性Name。所以現在我的SelectedPlatform.Keys是ControllerKey類型的通用列表,而不是字符串。這樣我克服了我的問題:) – muku 2011-02-03 15:53:52

2

1)帕夫洛Glazkov似乎有一個很好的答案我

2)這是下降到DataContext組合框現在是關鍵值對,而不是視圖模型。可能有其他方法可以做到這一點,但我之前使用過的方法是將綁定RelativeSource源設置回其父項ItemsControl。

RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}} 

喜歡的東西:

<ListBox Height="393" HorizontalAlignment="Left" Margin="0,72,0,0" Name="listBoxActions" VerticalAlignment="Top" Width="254" ItemsSource="{Binding Path=SelectedPlayer.ControlProfile.MappedActions}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> 
      <TextBox Text="{Binding Key, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
      <ComboBox Margin="10,0,0,0" Height="Auto" MinWidth="80" MaxWidth="80" HorizontalAlignment="Left" VerticalAlignment="Center" ItemsSource="{Binding DataContext.Keys, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" SelectedItem="{Binding Value, Mode=TwoWay}"/> 
     </StackPanel> 
     </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 
+0

所以你的代碼正在尋找列表框的項目。但那不是我需要的。如果您看到列表框的ItemsSource被綁定到SelectedPlayer.ControlProfile ... SelectedPlayer是我的viewmodel的一個屬性。我在viewmodel中有另一個名爲Keys的屬性。這就是我想要綁定組合框的ItemsSource的地方:) – muku 2011-02-03 12:45:25