「ItemsSource を使用する前に、Items コレクションを空にする必要があります。」私のコードのエラー
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でこの問題を解決するのを手伝ってください