2017-01-31 18 views
0

Я делаю SilverLight виджет, где потребуется пользователям, чтобы иметь возможность выбрать буквенные символы в сетке, как это:Используйте радиокнопки для выбора буквенных символов в Silverlight XAML()

enter image description here

И если я нажимаю на а в верхнем ряду, он должен выглядеть следующим образом:

enter image description here

в ViewModel она будет представлена ​​как строка, как chararray, так что 1 содержит массив а, яи 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> 

И затем есть конвертер для преобразования информации в строку. Но я не могу окунуться в голову, как это будет работать. Я надеюсь, что кто-то там с хорошей идеей :-)

ответ

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> 

И на вашем ПросмотрModel:

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(""); 
    }