【问题标题】:Setting the content property dynamically using a custom property XAML WPF使用自定义属性 XAML WPF 动态设置内容属性
【发布时间】:2012-10-03 10:46:32
【问题描述】:

我正在为触摸屏应用程序创建一个屏幕键盘,其中 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 .

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

        <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>
    

    【讨论】:

      【解决方案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)
      )]
      

      【讨论】:

        猜你喜欢
        • 2017-11-02
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多