1

我有以下场景:我有一个预定义的、已经初始化的Dictionary结构。该字典的元素通过ListBox显示。在每一行中,有两个TextBoxes和一个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; } }

我的问题/问题是:如何将单选按钮初始化为“已选中”?我使用此代码达到的行为是完整列表,显示了国家名称,并设置了变量。但是我的单选按钮都没有显示为选中状态。我想知道是否需要“刷新”页面,但如果需要,我该怎么做?

4

0 回答 0