将所选项目绑定到文本框

时间:2010-04-20 11:01:57

标签: wpf binding

嗨,任何人都可以告诉..how将列表框中的选定项目绑定到文本框.. 我使用了ElementName = Path = ..但没有显示任何内容......

2 个答案:

答案 0 :(得分:6)

这就是SelectedItem.Content的完成方式

<StackPanel>
<TextBlock Width="248" Height="24" Text="Colors:" 
    TextWrapping="Wrap"/>
<ListBox x:Name="lbColor" Width="248" Height="56">
    <ListBoxItem Content="Blue"/>
    <ListBoxItem Content="Green"/>
    <ListBoxItem Content="Yellow"/>
    <ListBoxItem Content="Red"/>
    <ListBoxItem Content="Purple"/>
    <ListBoxItem Content="Orange"/>
</ListBox>
<TextBlock Width="248" Height="24" Text="You selected color:" />
<TextBlock Width="248" Height="24">
    <TextBlock.Text>
        <Binding ElementName="lbColor" Path="SelectedItem.Content"/>
    </TextBlock.Text>
</TextBlock>

答案 1 :(得分:5)

通过Listbox的SelectedItem设置网格的Datacontext,然后执行正常的Binding,如下所示

 <Grid>
<Grid.ColumnDefinitions>
  <ColumnDefinition Width="3*"></ColumnDefinition>
  <ColumnDefinition Width="5*"></ColumnDefinition>
</Grid.ColumnDefinitions>

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>
  </Grid.RowDefinitions>

  <Button Margin="7,7,7,0" Padding="2" Click="cmdGetProducts_Click">Get Products</Button>
  <ListBox Grid.Row="1" Margin="7,3,7,10" Name="lstProducts" HorizontalContentAlignment="Stretch" SnapsToDevicePixels="True">
    <ListBox.ItemContainerStyle>
      <Style>
        <Setter Property="Control.Padding" Value="0"></Setter>
        <Style.Triggers>
          <Trigger Property="ListBoxItem.IsSelected" Value="True">
            <Setter Property="ListBoxItem.Background" Value="DarkRed" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </ListBox.ItemContainerStyle>     
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Grid Margin="0" Background="White">
        <Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue"
                CornerRadius="4">
          <Grid Margin="3">
            <Grid.RowDefinitions>
              <RowDefinition></RowDefinition>
              <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock FontWeight="Bold" Text="{Binding Path=ModelNumber}"></TextBlock>
            <TextBlock Grid.Row="1" Text="{Binding Path=ModelName}"></TextBlock>
          </Grid>
        </Border>
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>


</Grid>

<GridSplitter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Stretch"
               Width="5"></GridSplitter>

<Border Grid.Column="1" Padding="7" Margin="7" Background="LightSteelBlue">
  <Grid DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" >
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"></ColumnDefinition>
      <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    <TextBlock Margin="7">Model Number:</TextBlock>
    <TextBox Margin="5" Grid.Column="1" Text="{Binding Path=ModelNumber}"></TextBox>
    <TextBlock Margin="7" Grid.Row="1">Model Name:</TextBlock>
    <TextBox Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding Path=ModelName}"></TextBox>
    <TextBlock Margin="7" Grid.Row="2">Unit Cost:</TextBlock>
    <TextBox Margin="5" Grid.Row="2" Grid.Column="1" Text="{Binding Path=UnitCost}"></TextBox>
    <TextBlock Margin="7,7,7,0" Grid.Row="3">Description:</TextBlock>
    <TextBox Margin="7" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" 
             TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" Text="{Binding Path=Description}"></TextBox>

  </Grid>
</Border>