达普齐克斯
成员
- 加入
- 2014年10月22日
- 消息
- 7
- 编程经验
- 1-3
我是WPF的新手,我很难完成可能很简单的东西。
我的应用程序做了什么(或应该做)
从一串语言开始的字符串(在这种情况下西班牙语:"?????????"),我的应用程序是为每个角色显示一个按钮,以便用户可以按下按钮,并将字符插入它们所在的应用程序中。样式类似于通用字符插入工具栏。
到目前为止我的进展
我有按钮在网格中显示,当单击给定按钮时,将显示一个与该按钮关联的字符的消息框(示例,"?")。按钮是从数据模型动态创建的。
我的问题
我无法弄清楚如何将按钮标题/内容设置为单击时显示的相同字符。我当前的按钮XAML看起来如下所示:
模型内的数据如下:
输出是每个按钮都有标题"(Collection)"而不是适当的字符串值。
奖金问题
所有按钮目前都垂直放出,但我实际上希望水平设置最大的按钮,并将它们布置在2D网格中。任何指针都会很棒。
完整的代码
mainwindow.xaml.cs:
mainwindow.xaml:
和数据模型和辅助类,characterdatamodel.cs:
谢谢!
我的应用程序做了什么(或应该做)
从一串语言开始的字符串(在这种情况下西班牙语:"?????????"),我的应用程序是为每个角色显示一个按钮,以便用户可以按下按钮,并将字符插入它们所在的应用程序中。样式类似于通用字符插入工具栏。
到目前为止我的进展
我有按钮在网格中显示,当单击给定按钮时,将显示一个与该按钮关联的字符的消息框(示例,"?")。按钮是从数据模型动态创建的。
我的问题
我无法弄清楚如何将按钮标题/内容设置为单击时显示的相同字符。我当前的按钮XAML看起来如下所示:
C#:
<Button Content="{Binding DataContext.Data, RelativeSource={RelativeSource AncestorType=ItemsControl}}" Command="{Binding DataContext.InsertChar, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>
模型内的数据如下:
C#:
Data = new ObservableCollection<string>(Characters);
输出是每个按钮都有标题"(Collection)"而不是适当的字符串值。
奖金问题
所有按钮目前都垂直放出,但我实际上希望水平设置最大的按钮,并将它们布置在2D网格中。任何指针都会很棒。
完整的代码
mainwindow.xaml.cs:
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new CharacterDataModel();
}
}
C#:
Window x:Class="Special_Character_Inserter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Special Character Inserter" Height="300" Width="300">
<DockPanel>
<ItemsControl ItemsSource="{Binding Data}" Height="248" VerticalAlignment="Bottom">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" Background="Gainsboro" BorderThickness="1" Margin="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Content="{Binding DataContext.Data, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
Command="{Binding DataContext.InsertChar, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ScrollViewer CanContentScroll="True">
<Grid Height="248" Width="275">
<ItemsPresenter Grid.RowSpan="5" Grid.Row="5" Grid.ColumnSpan="5" Grid.Column="5"/>
</Grid>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DockPanel>
</Window>
C#:
class CharacterDataModel
{
const string csCharacters = "?????????";
/// <summary>
/// Fill character array with upper and lower versions of each character
/// </summary>
private string[] GetSpecialCharsFromString(string chars)
{
string[] RetVal = new string[chars.Length * 2];
for (int i = 0; i < chars.Length; i++)
{
string lowerChar = chars[i].ToString().ToLower();
RetVal[i * 2] = lowerChar;
string upperChar = lowerChar.ToUpper();
if (lowerChar != upperChar)
RetVal[i * 2 + 1] = upperChar;
}
return RetVal;
}
public Command<string> InsertChar { get; set; }
public ObservableCollection<string> Data { get; set; }
public CharacterDataModel()
{
string[] Characters = GetSpecialCharsFromString(csCharacters);
Data = new ObservableCollection<string>(Characters);
InsertChar = new Command<string>(ExecuteCommand);
}
private void ExecuteCommand(string data)
{
MessageBox.Show(data);
}
}
public class Command : ICommand
{
public Action Action { get; set; }
public string DisplayName { get; set; }
public void Execute(object parameter)
{
if (Action != null)
Action();
}
public bool CanExecute(object parameter) => IsEnabled;
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
public event EventHandler CanExecuteChanged;
public Command(Action action)
{
Action = action;
}
}
public class Command<T> : ICommand
{
public Action<T> Action { get; set; }
public void Execute(object parameter)
{
if (Action != null && parameter is T)
Action((T)parameter);
}
public bool CanExecute(object parameter)
{
return IsEnabled;
}
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
public event EventHandler CanExecuteChanged;
public Command(Action<T> action)
{
Action = action;
}
}