绑定控件属性以在运行时更改它

时间:2014-08-19 10:55:08

标签: c# wpf c#-4.0 wpf-controls

我正在为我的项目准备某种字母数字键盘控制。不幸的是,我遇到了在运行时动态更改按钮内容的问题。这是一个示例代码:

XAML

<UserControl x:Class="WpfApplication10.AlphaNumericKeyboard"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication10"
         mc:Ignorable="d" 
         DataContext="{Binding RelativeSource={RelativeSource Self}}"
         d:DesignHeight="300" d:DesignWidth="800">
<UserControl.Resources>
    <local:LowerUpperCaseConverter x:Key="LowerUpperCaseConverter"/>
    <Style TargetType="Button" x:Key="KeyboardButton">
        <Setter Property="Width" Value="30"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="Background" Value="Black" />
        <Setter Property="Foreground" Value="White" />
    </Style>
    <!--<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />-->
    <Style x:Key="KeyboardStyle" TargetType="{x:Type StackPanel}">
        <Style.Resources>
            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource KeyboardButton}"/>
        </Style.Resources>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
    </Style>

</UserControl.Resources>
<StackPanel Background="Red" Style="{DynamicResource KeyboardStyle}">
    <TextBox />
    <StackPanel Orientation="Horizontal">
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=q, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=w, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=e, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=r, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=t, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=y, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=u, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=i, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=o, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=p, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=a, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=s, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=d, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=f, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=g, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=h, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=j, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=k, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=l, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Button Content="↑" Click="Button_Click"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=z, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=x, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=c, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=v, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=b, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=n, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
        <Button Content="{Binding Path=IsUppercase, ConverterParameter=m, Converter={StaticResource LowerUpperCaseConverter}, Mode=TwoWay}"/>
    </StackPanel>
</StackPanel>

CS

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfApplication10
{
/// <summary>
/// Interaction logic for AlphaNumericKeyboard.xaml
/// </summary>
public partial class AlphaNumericKeyboard : UserControl
{
    public bool IsUppercase { get; set; }

    public AlphaNumericKeyboard()
    {
        //this.IsUppercase = true;
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.IsUppercase = !this.IsUppercase;
        this.UpdateLayout();
    }
}

//[ValueConversion(typeof(bool), typeof(string))]
public class LowerUpperCaseConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return (bool)value ? parameter.ToString().ToUpper() : parameter.ToString().ToLower();
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return true;
    }
}

}

运行时它正常工作。它会根据IsUppercase属性在按钮上显示大写或小写字母,但前提是它在InitializeComponents();之前设置。之后,当我通过单击向上箭头符号更改IsUppercase属性时,它似乎没有任何效果。

所以我的问题是:如何绑定控件属性以在运行时更改它?

1 个答案:

答案 0 :(得分:2)

您需要实现DependencyProperty或实施INotifyPropertyChanged interface以从后面的代码更新UI控件。

试试这个:

public partial class AlphaNumericKeyboard : UserControl, INotifyPropertyChanged
{
    private bool isUppercase;
    public bool IsUppercase
    {
        get { return isUppercase; }
        set
        {
            isUppercase = value; 
            NotifyPropertyChanged("IsUppercase");
        }
    }

    ...

    /// Implement INotifyPropertyChanged interface correctly
}

请点击该链接,了解如何正确实施INotifyPropertyChanged界面。