C# 绑定控件属性以在运行时对其进行更改
我正在为我的项目准备一个字母数字键盘控件。不幸的是,我在运行时动态更改按钮的内容时遇到了一个问题。下面是一个示例代码: XAMLC# 绑定控件属性以在运行时对其进行更改,c#,wpf,c#-4.0,wpf-controls,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
<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;
}
}
使用系统;
使用System.Collections.Generic;
利用制度全球化;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
命名空间WpfApplication10
{
///
///AlphaNumericKeyboard.xaml的交互逻辑
///
公共部分类AlphaNumericKeyboard:UserControl
{
公共bool为大写{get;set;}
公共字母数字键盘()
{
//this.IsUppercase=true;
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
this.IsUppercase=!this.IsUppercase;
this.UpdateLayout();
}
}
//[ValueConversion(typeof(bool)、typeof(string))]
公共类LowerUpperCaseConverter:IValueConverter
{
公共对象转换(对象值,类型targetType,
对象参数,CultureInfo(区域性)
{
返回(bool)值?parameter.ToString().ToUpper():parameter.ToString().ToLower();
}
公共对象转换回(对象值,类型targetType,
对象参数,CultureInfo(区域性)
{
返回true;
}
}
}
当我运行它时,它工作正常。它在按钮上显示大写或小写字母,具体取决于IsUppercase
属性,但只有在InitializeComponents()之前设置代码>。之后,当我通过单击向上箭头符号来更改IsUppercase
属性时,它似乎没有任何效果
因此,我的问题是:如何绑定控件属性以在运行时对其进行更改?您需要实现或实现来从代码隐藏更新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
界面。您需要实现或实现以从代码隐藏更新UI控件。谢谢,我花了几个小时寻找该解决方案。然后,它可能会帮助您通读MSDN上的页面。