在列表框中绑定itemtemplate问题

时间:2011-02-03 11:49:52

标签: wpf binding itemtemplate

我在列表框中有两个单独的绑定问题,其中包含一个包含texbox的itemtemplate。

1)一个列表框绑定到字符串列表。如何在创建的文本框中显示每个字符串,并允许同时进行双向绑定?如果不指定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对象的键属性,组合框选择项绑定到value属性。我的问题是我希望组合由我的viewmodel中声明的字符串列表填充,该列表将在运行时更改。窗口的datacontext是声明列表的viewmodel。我不知道我需要用于绑定组合框项目源的确切语法。这是我的代码:

<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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

1)Pavlo Glazkov似乎对我有一个很好的答案

2)这是由于ComboBox现在是键值对而不是ViewModel的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>