2012-10-03 5 views
2

Я создаю экранную клавиатуру для приложения с сенсорным экраном, где shift переключает верхние и нижние регистровые кнопки на всей клавиатуре.Динамическое изменение свойства содержимого с помощью настраиваемого свойства XAML WPF

Код в C# работает, но я не знаю, как изменить значение содержимого и параметр команды кнопок на основе моего настраиваемого свойства, которое изменяется на значение bool в xaml.

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

Это то, что я в настоящее время для каждой кнопки в XAML (игнорировать содержание, так как я ухватиться за соломинку здесь), идея заключается в том, что смена ключа будет переключать контент и 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 в папке Themes)

<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) 
)] 
Смежные вопросы