次のシナリオがあります: 定義済みの、既に初期化されたDictionary構造があります。このディクショナリの要素は、ListBoxを介して表示されます。各行には、2 つのTextBoxと 1 つのRadioButtonがあり、辞書内の Country 構造変数を参照します。ラジオボタンをクリックすると、国名が環境設定に保存され、ビューをリロードすると、保存されたオプションが復元されます。私のコードは次のとおりです。
.XAML ファイル:
<ListBox Height="Auto" Margin="0, 0, 0, 16" x:Name="CountryCodeSelect_countrylist">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="CountryCodeSelect_listfields" Margin="16" Height="64" Width="432" Background="#F2F2F2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock
Margin="16,0,0,0"
Text="{Binding Key}"
Foreground="Black"
FontSize="24"
FontWeight="Black"
VerticalAlignment="Center"
Grid.Row="0" Grid.Column="0"/>
<TextBlock
Margin="16,0,8,0"
Text="{Binding Value.CountryCode}"
Foreground="Black"
FontSize="24"
FontWeight="Black"
VerticalAlignment="Center"
Grid.Row="0" Grid.Column="1"/>
<RadioButton
Margin="0,0,24,0"
IsChecked="{Binding Path=IsChecked, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=Value.CountryName}"
GroupName="CountryCodeSelect_countries"
DataContext="{Binding Value.CountryName}"
Grid.Row="0" Grid.Column="2"
HorizontalAlignment="Right"
Click="RadioButton_Click" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
そして、私のモデル .CS ファイル:
public Dictionary<string, Country> countries = new Dictionary<string, Country>() {
{ "Afghanistan", new Country("Afghanistan", ".com", 93, false) },
{ "Albania", new Country("Albania", ".com", 355, false) },
{ "Algeria", new Country("Algeria", ".com", 213, false) },
. . .
. . .
. .
.
}
public CountryCodeSelect() {
InitializeComponent();
// reload previous saved value
string cn = Persistence.load_countryName();
if (cn != null) {
Country c;
countries.TryGetValue(cn, out c);
c.Checked = true;
countries.Remove(c.CountryName);
countries.Add(c.CountryName, c);
}
CountryCodeSelect_countrylist.ItemsSource = countries;
}
private int _IsChecked;
public int IsChecked { get { return _IsChecked; } set { _IsChecked = value; } }
私の問題/質問は、ラジオボタンを「チェック済み」として初期化するにはどうすればよいですか? このコードで到達した動作は、国名が表示され、変数が設定された完全なリストです。しかし、私のラジオボタンはどれもチェック済みとして表示されていません。ページを「更新」する必要があるかどうか疑問に思っていますが、必要な場合はどうすればよいですか?