如何根据数据源设置网格中重复按钮的内容

迪皮克

成员
已加入
2014年10月22日
留言内容
7
编程经验
1-3
我是WPF的新手,很难完成可能很简单的事情。

我的应用程序做什么(或应该做什么)
以一串特殊的语言字符开头(在这种情况下为西班牙语:"?????????"),我的应用程序是为每个字符显示一个按钮,以便用户可以按该按钮并将字符插入到其所在的任何应用程序中。有点像通用字符插入工具栏。

到目前为止我的进度
我有显示在网格中的按钮,当您单击给定的按钮时,会出现一个消息框,其中包含与该按钮关联的字符(例如,"?")。这些按钮是根据数据模型动态创建的。

我的问题
我不知道如何将按钮标题/内容设置为单击时显示的相同字符。我当前针对该按钮的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();     
        }
    }
MainWindow.xaml:
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>
以及数据模型和辅助类CharacterDataModel.cs:
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;
        }
    }
谢谢!
 
最佳 底部