使用按钮同步列表框中的数据模板

时间:2018-01-22 00:38:26

标签: c# xaml data-binding listbox

<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:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="740">
<Window.Resources>
    <local:Leute x:Key="freunde"/>
    <DataTemplate x:Key="detail">

            <StackPanel  FlowDirection="LeftToRight" Orientation="Horizontal">
            <TextBlock x:Name="tb2"  Text="{Binding Path=Vorname}"></TextBlock>
            <TextBlock  Text="{Binding Path=Nachname}"></TextBlock>
            <TextBlock  Text="{Binding Path=Geburtsdatum, StringFormat = dd.MM.yy}"></TextBlock>
            </StackPanel>

    </DataTemplate>


</Window.Resources>
<Grid DataContext="{StaticResource freunde}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition  />
        <ColumnDefinition  />
    </Grid.ColumnDefinitions>

    <ListBox ItemTemplate="{StaticResource detail}" ItemsSource="{StaticResource freunde}"/>



    <TextBlock Grid.Column="1" Text="Vorname" Margin="0,23,254,265"></TextBlock>
    <TextBlock Grid.Column="1" Text="Nachname" Margin="0,72,290,221"></TextBlock>
    <TextBlock Grid.Column="1" Text="Geburtsdatum" Margin="0,121,254,162" RenderTransformOrigin="0.086,1.118"></TextBlock>
    <TextBox  Grid.Column="1" Text="{Binding Path=Vorname}" Margin="122,28,110,261"/>
    <TextBox Grid.Column="1" Text="{Binding Path=Nachname}" Margin="122,71,110,221"/>
    <TextBox Grid.Column="1" Text="{Binding Path=Geburtsdatum, StringFormat = dd.MM.yy}" Margin="122,120,110,162"/>

</Grid>

您好,

我正在学习测试,这是它的一项任务。如果我在列表框中选择一个项目,它将不会显示在文本框中。尝试了一些像元素名称之类的东西,但它没有成功。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

解决了它

  IsSynchronizedWithCurrentItem="true"

在这里失踪:

 <ListBox ItemTemplate="{StaticResource detail}" ItemsSource="{StaticResource freunde}" IsSynchronizedWithCurrentItem="True" x:Name="list" />