我有“在使用ItemsSource之前,项目集合必须为空。”我的代码出错
我在StackOverFlow链接中测试所有答案但是他们没有工作! 我的Xaml:
<Window x:Class="FirstWpfTestApplication.Main"
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:FirstWpfTestApplication"
xmlns:valConverter="clr-namespace:FirstWpfTestApplication.Model.ValueConverters"
mc:Ignorable="d"
Title="Main" Height="500" Width="800" WindowStartupLocation="CenterScreen">
<UserControl>
<UserControl.Resources>
<valConverter:GenderConverter x:Key="GenderConverter"/>
<valConverter:EnumList x:Key="EnumConverter"/>
<Style TargetType="TextBox">
<Setter Property="MinWidth" Value="100"/>
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="RadioButton">
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="CheckBox">
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="MinWidth" Value="100"/>
</Style>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="MinWidth" Value="60"/>
</Style>
</UserControl.Resources>
<Grid>
<DockPanel>
<!--<ListBox Name="lstPeople" DockPanel.Dock="Left">
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:" Margin="10 0"/>
<TextBlock Text="{Binding Name}" Margin="10 0"/>
</StackPanel>
</DataTemplate>
</ListBox>-->
<Border BorderThickness="2" BorderBrush="Black" CornerRadius="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0" Margin="10">
<TextBlock>Name:</TextBlock>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="1" Margin="10">
<TextBlock VerticalAlignment="Center">Gender:</TextBlock>
<WrapPanel Orientation="Vertical">
<RadioButton IsChecked="{Binding Gender,Converter={StaticResource GenderConverter},ConverterParameter=True}"
Name="rdMan">
Man
</RadioButton>
<RadioButton IsChecked="{Binding Gender,Converter={StaticResource GenderConverter},ConverterParameter=False}"
Name="rdWoman">
WoMan
</RadioButton>
</WrapPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="2" Margin="10">
<TextBlock>Field Of Work:</TextBlock>
<StackPanel Orientation="Horizontal">
<CheckBox Name="chActor" IsChecked="{Binding FieldOfWorks,Converter={StaticResource EnumConverter},ConverterParameter=Actor}">
Actor
</CheckBox>
<CheckBox Name="chDirector" IsChecked="{Binding FieldOfWorks,Converter={StaticResource EnumConverter},ConverterParameter=Director}">
Director
</CheckBox>
<CheckBox Name="chProducer" IsChecked="{Binding FieldOfWorks,Converter={StaticResource EnumConverter},ConverterParameter=Producer}">
Producer
</CheckBox>
</StackPanel>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<TextBlock>Country:</TextBlock>
<ComboBox Name="cbCountries">
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox>
</StackPanel>
<StackPanel Margin="0 10" HorizontalAlignment="Left" Orientation="Horizontal" Grid.Row="4">
<Button>Save</Button>
</StackPanel>
</Grid>
</Border>
</DockPanel>
</Grid>
</UserControl>
和我的C#代码:
public partial class Main : Window
{
public List<People> PeopleContext;
public Main()
{
InitializeComponent();
this.DataContext = People.GetPeople();
cbCountries.ItemsSource = new Country().GetCountries();//In This Line Exception Will Throw
}
}
请帮助解决WPF中的此问题
答案 0 :(得分:1)
你的组合框直接使用DataTemplate。而是放在ItemTemplate属性中。请参阅下面的代码。
<ComboBox Name="cbCountries">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>