简体   繁体   English

如何修复 ContentControl 绑定

[英]How to fix ContentControl binding

I have a very simple program in which a ListBox contains the name of three people.我有一个非常简单的程序,其中一个 ListBox 包含三个人的名字。 The program should show that person's information (name, age and colour) in the area at the bottom, when you select the name of a person in ListBox.当您在 ListBox 中输入 select 某个人的姓名时,该程序应在底部区域显示该人的信息(姓名、年龄和颜色)。 It should, but it doesn't display it:它应该,但它不显示它:

In the resource section I have a declared a collection of Person class and a DataTemplate that defines how the information of a Person is presented.在资源部分,我声明了一个 Person class 的集合和一个定义如何呈现 Person 信息的 DataTemplate。

The ListBox, listBoxMyFriends, and TextBlock, textBlockMyFriends, bind to the same source (MyFriends), with ListBox using ItemsSource property and TextBlock using ContentControl. ListBox、listBoxMyFriends 和 TextBlock、textBlockMyFriends 绑定到相同的源 (MyFriends),其中 ListBox 使用 ItemsSource 属性,TextBlock 使用 ContentControl。
The ContentControl should show the other properties of the person selected. ContentControl 应显示所选人员的其他属性。

XAML: XAML:

<Window x:Class="WpfApp1.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:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <local:Person x:Key="MyFriends" />
    <DataTemplate x:Key="DetailTemplate">
        <Border Width="300" Height="100" Margin="20"
      BorderBrush="Black" BorderThickness="1" Padding="8">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="Name:"/>
                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=PersonName}"/>
                <TextBlock Grid.Row="1" Grid.Column="0" Text="Age:"/>
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=PersonAge}"/>
                <TextBlock Grid.Row="2" Grid.Column="0" Text="Colour"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=PersonColour}"/>
            </Grid>
        </Border>
    </DataTemplate>
</Window.Resources>

<StackPanel>
    <TextBlock FontFamily="Verdana" FontSize="11"
           Margin="5,15,0,10" FontWeight="Bold">My Friends:</TextBlock>
    <ListBox Width="200" IsSynchronizedWithCurrentItem="True" Name="listBoxMyFriends"
         ItemsSource="{Binding Source={StaticResource MyFriends}}"/>
    <TextBlock FontFamily="Verdana" FontSize="11" Name="textBlockMyFriends"
           Margin="5,15,0,5" FontWeight="Bold">Information:</TextBlock>
    <ContentControl Content="{Binding Source={StaticResource MyFriends}}"
                ContentTemplate="{StaticResource DetailTemplate}"/>
</StackPanel>

Code Behind:代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Person[] person = { new Person("Bob", 99, "red"),
                            new Person("John", 20, "green"),
                            new Person("Mike", 30, "blue")
        };
        listBoxMyFriends.ItemsSource = person;
    }
}

Person cs.:人称:

public class Person
{
    public string PersonName { get; set; }
    public int PersonAge { get; set; }
    public string PersonColour { get; set; }

    public Person() { }

    public Person(string name, int age, string colour)
    {
        PersonName = name;
        PersonAge = age;
        PersonColour = colour;
    }

    public override string ToString()
    {
        return PersonName.ToString();
    }

}

The information is not displaying when I try to select a person.当我尝试 select 一个人时,信息没有显示。 I think I'm missing something really simple with ContentControl, but I can't figure out what that is.我想我在 ContentControl 中遗漏了一些非常简单的东西,但我不知道那是什么。

ContentControl works properly with data it has, but you give it wrong data. ContentControl 可以正常处理它拥有的数据,但您给它的数据是错误的。

you should bind ContentControl.Content to ListBox.SelectedItem:您应该将 ContentControl.Content 绑定到 ListBox.SelectedItem:

<Window.Resources>
    <DataTemplate x:Key="DetailTemplate">
         ...
    </DataTemplate>
</Window.Resources>

<StackPanel>
    <TextBlock FontFamily="Verdana" FontSize="11"
           Margin="5,15,0,10" FontWeight="Bold">My Friends:</TextBlock>
    <ListBox Width="200" IsSynchronizedWithCurrentItem="True" Name="listBoxMyFriends" />

    <TextBlock FontFamily="Verdana" FontSize="11" Name="textBlockMyFriends"
           Margin="5,15,0,5" FontWeight="Bold">Information:</TextBlock>
    <ContentControl Content="{Binding Path=SelectedItem, ElementName=listBoxMyFriends}"
                ContentTemplate="{StaticResource DetailTemplate}"/>
</StackPanel>

resource <local:Person x:Key="MyFriends" /> is redundant.资源<local:Person x:Key="MyFriends" />是多余的。 it is just empty instance of Person class, not present in collection.它只是 Person class 的空实例,不存在于集合中。 so no reason to display it.所以没有理由展示它。

data for display is set in code-behind ( listBoxMyFriends.ItemsSource = person; )用于显示的数据在代码隐藏中设置( listBoxMyFriends.ItemsSource = person;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM