我有一個非常簡單的程式,其中一個 ListBox 包含三個人的名字。當您在 ListBox 中選擇一個人的姓名時,該程式應在底部區域顯示該人的資訊(姓名、年齡和顏色)。它應該,但它不顯示它:
在資源部分,我宣告了一個 Person 類的集合和一個 DataTemplate,它定義了如何呈現 Person 的資訊。
ListBox、listBoxMyFriends 和 TextBlock、textBlockMyFriends 系結到同一個源 (MyFriends),其中 ListBox 使用 ItemsSource 屬性,TextBlock 使用 ContentControl。
ContentControl 應顯示所選人員的其他屬性。
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>
代碼背后:
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;
}
}
人選:
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();
}
}
當我嘗試選擇一個人時,該資訊未顯示。我想我錯過了 ContentControl 的一些非常簡單的東西,但我不知道那是什么。
uj5u.com熱心網友回復:
ContentControl 可以正常使用它擁有的資料,但是你給它錯誤的資料。
您應該將 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>
資源<local:Person x:Key="MyFriends" />是多余的。它只是 Person 類的空實體,不存在于集合中。所以沒有理由顯示它。
用于顯示的資料在代碼隱藏 ( listBoxMyFriends.ItemsSource = person;)中設定
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/513323.html
標籤:C#wpf内容控制
