在 silverlight 中使用单选按钮 select 字母字符 (xaml)
Use radioButtons to select alphabetic characters in silverlight (xaml)
我正在制作一个 silverlight 小部件,我需要用户能够在网格中 select 字母字符,如下所示:
如果我点击第一行的 a,它应该是这样的:
在视图模型中,它将像字符数组一样表示为字符串,因此 1 包含一个包含 a、i 的数组,2 包含一个包含 d、h 等的数组。
现在,我的问题是,最好的表示方式是什么?
我最初的想法是让单选按钮按字母分组。
<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>
然后有一个转换器将信息转换为字符串。但我无法理解这将如何运作。我希望有人有好主意:-)
我相信有更好的方法来做到这一点,但这个方法很有效,使用命令而不是转换器:
<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("");
}
我正在制作一个 silverlight 小部件,我需要用户能够在网格中 select 字母字符,如下所示:
如果我点击第一行的 a,它应该是这样的:
在视图模型中,它将像字符数组一样表示为字符串,因此 1 包含一个包含 a、i 的数组,2 包含一个包含 d、h 等的数组。
现在,我的问题是,最好的表示方式是什么?
我最初的想法是让单选按钮按字母分组。
<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>
然后有一个转换器将信息转换为字符串。但我无法理解这将如何运作。我希望有人有好主意:-)
我相信有更好的方法来做到这一点,但这个方法很有效,使用命令而不是转换器:
<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("");
}