2

我正在为触摸屏应用程序创建一个屏幕键盘,其中 shift 切换整个键盘上的大写和小写按钮。

c# 中的代码正在工作,但我不知道如何根据我的自定义属性更改按钮的内容值和命令参数,该自定义属性在 xaml 中更改为 bool 值。

<local:KeyboardButton Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Command="{Binding AddText}" Content ="{Binding local:KeyboardButton.SelectedKey}" LowerCaseKey="`" UpperCasekey="¬"/>

这是我目前为 XAML 中的每个按钮所拥有的(忽略内容,因为我在这里一直在抓住稻草),想法是 shift 键将在 LowerCaseKey 和 UpperCaseKey 属性之间切换 Content 和 CommandParameter。

4

2 回答 2

2

也许您可以通过样式和触发器来实现您的目标:

    <Button Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Command="{Binding AddText}" x:Name="AButton">
        <Button.Resources>
            <Style TargetType="Button">
                <Setter Property="Content" Value="{Binding Path=LowerCaseKey, ElementName=AButton}" />
                <Setter Property="CommandParameter" Value="{Binding Path=LowerCaseKey, ElementName=AButton}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsUpperCase}" Value="true">
                        <Setter Property="Content" Value="{Binding Path=UpperCasekey, ElementName=AButton}" />
                        <Setter Property="CommandParameter" Value="{Binding Path=UpperCasekey, ElementName=AButton}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Resources>
    </Button>
于 2012-10-03T12:39:08.247 回答
2

自定义控件:

using System.Windows;
using System.Windows.Controls;

namespace Test
{
    public class KeyboardButton : Button
    {
        public static readonly DependencyProperty SelectedKeyProperty = DependencyProperty.Register("SelectedKey", typeof(string),
            typeof(KeyboardButton), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsArrange));

        public static readonly DependencyProperty IsUpperCaseProperty = DependencyProperty.Register("IsUpperCase", typeof(bool),
            typeof(KeyboardButton), new FrameworkPropertyMetadata(false));

        static KeyboardButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(KeyboardButton), new FrameworkPropertyMetadata(typeof(KeyboardButton)));
        }


        public string SelectedKey
        {
            get { return (string)GetValue(SelectedKeyProperty); }
            set { SetValue(SelectedKeyProperty, value); }
        }


        public string LowerCaseKey
        {
            get;
            set;
        }

        public string UpperCaseKey
        {
            get;
            set;
        }

        public bool IsUpperCase
        {
            get { return (bool)GetValue(IsUpperCaseProperty); }
            set { SetValue(IsUpperCaseProperty, value); }
        }
    }
}

Themes\Generic.xaml(主题文件夹中的文件 Generic.xaml)

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test">


    <Style TargetType="{x:Type local:KeyboardButton}" BasedOn="{StaticResource {x:Type Button}}"> 
        <Setter Property="Content" Value="{Binding LowerCaseKey, Mode=OneTime, RelativeSource={RelativeSource Self}}"/>
        <Style.Triggers>
            <Trigger Property="IsUpperCase" Value="true">
                <Setter Property="Content" Value="{Binding UpperCaseKey, Mode=OneTime, RelativeSource={RelativeSource Self}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

不要在 AssemblyInfo.cs 中忘记这一点:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]
于 2012-10-03T14:07:17.070 回答