C# 在WPF中实现转换器的自定义TextBox用户控件
我正试图构建一个自定义用户控件,确切地说是一个自定义C# 在WPF中实现转换器的自定义TextBox用户控件,c#,wpf,data-binding,user-controls,dependency-properties,C#,Wpf,Data Binding,User Controls,Dependency Properties,我正试图构建一个自定义用户控件,确切地说是一个自定义文本框。这个想法是当用户在文本框中键入时,文本将转换为大写并显示。但是我不能让它工作 CustomTextBox用户控件: <UserControl x:Class="SOFWpf.CustomTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas
文本框
。这个想法是当用户在文本框中键入时,文本将转换为大写并显示。但是我不能让它工作
CustomTextBox用户控件:
<UserControl x:Class="SOFWpf.CustomTextBox"
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:converters="clr-namespace:SOFWpf.Converters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<converters:CaseConverter x:Key="CaseConverter" />
</UserControl.Resources>
<TextBox Text="{Binding Path=., Converter={StaticResource CaseConverter}}"/>
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(CustomTextBox), new UIPropertyMetadata(""));
<local:CustomTextBox Text="a b c"/>
public class CaseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = value as string;
return text?.ToUpper();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = value as string;
return text?.ToLower();
}
}
用法:
<UserControl x:Class="SOFWpf.CustomTextBox"
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:converters="clr-namespace:SOFWpf.Converters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<converters:CaseConverter x:Key="CaseConverter" />
</UserControl.Resources>
<TextBox Text="{Binding Path=., Converter={StaticResource CaseConverter}}"/>
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(CustomTextBox), new UIPropertyMetadata(""));
<local:CustomTextBox Text="a b c"/>
public class CaseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = value as string;
return text?.ToUpper();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = value as string;
return text?.ToLower();
}
}
您需要添加绑定路径=文本:
<TextBox Text="{Binding Path=Text, Converter={StaticResource CaseConverter}}"/>
您需要添加绑定路径=文本:
<TextBox Text="{Binding Path=Text, Converter={StaticResource CaseConverter}}"/>
您希望显示的文本仅为大写还是希望文本块后面的实际数据也为大写?不,仅显示它!您至少需要将TextBox的Text属性绑定到UserControl的Text属性,比如
@Vahid set Mode=TwoWay和UpdateSourceTrigger asPropertyChanged@WPFUser成功了!您希望显示的文本仅为大写还是希望文本块后面的实际数据也为大写?不,仅显示它!您至少需要将TextBox的Text属性绑定到UserControl的Text属性,比如
@Vahid set Mode=TwoWay和UpdateSourceTrigger asPropertyChanged@WPFUser成功了!