2012-10-03 113 views
2

我正在爲觸摸屏應用程序創建屏幕鍵盤,在該鍵盤上切換整個鍵盤上的大寫和小寫按鈕切換。使用自定義屬性動態設置內容屬性XAML WPF

c#中的代碼正在工作,但我不知道如何根據我的自定義屬性更改布爾值xaml中的按鈕的內容值和命令參數。

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

這是我目前在XAML每個按鈕(忽略的內容,因爲我已經在這裏救命稻草抓),這個想法是,shift鍵將切換之間的內容和CommandParameter LowerCaseKey和UpperCaseKey屬性。

回答

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

謝謝,它的工作。 – Lewis

1

自定義控件:

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); } 
     } 
    } 
} 

主題\ 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) 
)]