使用radioButtons在silverlight(xaml)中选择字母字符

时间:2017-01-31 10:55:33

标签: c# xaml silverlight radio-button converter

我正在创建一个silverlight小部件,我需要用户能够在网格中选择字母字符,如下所示:

enter image description here

如果我点击顶行的a,它将如下所示:

enter image description here

在viewmodel中,它将像一个字符串一样表示为chararray,因此1包含一个带有a的数组,i和2包含一个带有d,h的数组,依此类推。

现在,我的问题是,代表这个的最佳方式是什么?

我最初的想法是拥有按字母分组的radioButtons。

<RadioButton GroupName="a" Grid.Column="1" Grid.Row="1" IsChecked="{Binding 1, Mode=TwoWay, Converter={StaticResource BSMClassConverter}, ConverterParameter='a'}" ></RadioButton>
<RadioButton GroupName="a" Grid.Column="1" Grid.Row="2" IsChecked="{Binding 2, Mode=TwoWay, Converter={StaticResource BSMClassConverter}, ConverterParameter='a'}" ></RadioButton>
<RadioButton GroupName="a" Grid.Column="1" Grid.Row="3" IsChecked="{Binding 3, Mode=TwoWay, Converter={StaticResource BSMClassConverter}, ConverterParameter='a'}" ></RadioButton>

         

然后有一个转换器将信息转换为字符串。但我无法理解这是如何发挥作用的。我希望有一个有好主意的人:-)

1 个答案:

答案 0 :(得分:2)

我确定有更好的方法可以做到这一点,但这可行,使用命令而不是转换器:

    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <RadioButton Content="a" GroupName="a" Grid.Row="0" Grid.Column="0" Command="{Binding UpdateSelection}" CommandParameter="1;a" />
    <RadioButton Content="a" GroupName="a" Grid.Row="1" Grid.Column="0" Command="{Binding UpdateSelection}" CommandParameter="2;a" />
    <RadioButton Content="a" GroupName="a" Grid.Row="2" Grid.Column="0" Command="{Binding UpdateSelection}" CommandParameter="3;a" />

    <RadioButton Content="b" GroupName="B" Grid.Row="0" Grid.Column="1" Command="{Binding UpdateSelection}" CommandParameter="1;b" />
    <RadioButton Content="b" GroupName="B" Grid.Row="1" Grid.Column="1" Command="{Binding UpdateSelection}" CommandParameter="2;b" />
    <RadioButton Content="b" GroupName="B" Grid.Row="2" Grid.Column="1" Command="{Binding UpdateSelection}" CommandParameter="3;b" />

    <RadioButton Content="c" GroupName="C" Grid.Row="0" Grid.Column="2" Command="{Binding UpdateSelection}" CommandParameter="1;c" />
    <RadioButton Content="c" GroupName="C" Grid.Row="1" Grid.Column="2" Command="{Binding UpdateSelection}" CommandParameter="3;c" />
    <RadioButton Content="c" GroupName="C" Grid.Row="2" Grid.Column="2" Command="{Binding UpdateSelection}" CommandParameter="3;c" />

</Grid>

在你的ViewModel中:

List<string> row1List = new List<string>();
    List<string> row2List = new List<string>();
    List<string> row3List = new List<string>();

    public RelayCommand<string> UpdateSelection { get; private set; }

    public MainViewModel()
    {
        UpdateSelection = new RelayCommand<string>((str) => UpdateSelectionExecute(str));
    }

    private void UpdateSelectionExecute(string str)
    {
        string[] split = str.Split(';');

        switch (split[0])
        {
            case "1":
                Remove(split[1]);
                row1List.Add(split[1]);
                break;
            case "2":
                Remove(split[1]);
                row2List.Add(split[1]);
                break;
            case "3":
                Remove(split[1]);
                row3List.Add(split[1]);
                break;
        }

        OutputToConsole();

    }

    private void Remove(string character)
    {
        if (row1List.Contains(character))
        {
            row1List.Remove(character);
        }

        if (row2List.Contains(character))
        {
            row2List.Remove(character);
        }

        if (row3List.Contains(character))
        {
            row3List.Remove(character);
        }
    }  

    private void OutputToConsole()
    {
        Console.WriteLine("List 1: ");
        row1List.OrderBy(o => o).ToList().ForEach((c) => Console.Write(c));
        Console.WriteLine("");


        Console.WriteLine("List 2: ");
        row2List.OrderBy(o => o).ToList().ForEach((c) => Console.Write(c));
        Console.WriteLine("");


        Console.WriteLine("List 3: ");
        row3List.OrderBy(o => o).ToList().ForEach((c) => Console.Write(c));
        Console.WriteLine("");
    }