绑定不会更新用户控件

绑定不会更新用户控件

本文介绍了绑定不会更新用户控件[UWP] [XAML]上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个UserControl,它只是一个带有一些额外代码的文本框,可以实现模板中我想要的行为。


我的问题是当我绑定时对于ViewModel上的属性,文本框不显示信息。


UserControl的Codebehind

 // /< summary> 
///包含Float TextBox的文本
///< / summary>
public string Text
{
get {return(string)GetValue(TextProperty); }
设置
{
SetValue(TextProperty,value);
NotifyPropertyChanged(" Text");
}
}

///< summary>
///用于保存Float TextBox文本的依赖项属性。
///< / summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(" Text",typeof(string),typeof(FloatTextBox),null);




UserControl的XAML文件

< TextBox x:Name =" TextBox" 
GotFocus =" TextBox_GotFocus"
LostFocus =" TextBox_LostFocus"
TextChanged =" TextBox_TextChanged"
PointerEntered =" TextBox_PointerEntered"
PointerExited =" TextBox_PointerExited"
PointerPressed =" TextBox_PointerPressed"
PointerReleased =" TextBox_PointerReleased"
PlaceholderText =" {Binding PlaceholderText}"
Header =" {Binding PlaceholderText}"
TextWrapping =" {Binding TextWrapping}"
Text =" {Binding Text,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}"
IsReadOnly =" {Binding IsReadOnly}"
BorderThickness =" {Binding BorderThickness}"
BorderBrush =" {Binding BorderBrush}"
AcceptsReturn =" {Binding AcceptsReturn}"
MaxLength =" {Binding MaxLength}" />

我正在测试控件的页面

< Page 
x:Class =" CustomControlsTestApp.MainPage"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local =" using:CustomControlsTestApp"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:customControls =" using:CustomControlLibrary"
mc:Ignorable =" d">

< Grid Background =" AliceBlue">
< StackPanel Orientation =" Vertical">
< TextBlock Text =" PasswordBox"
Margin =" 0,20,0,0" />
< customControls:FloatPasswordBox PlaceholderHeaderText ="密码"
密码="测试" />
< TextBlock Text =" Float TextBox"
Margin =" 0,20,0,0" />
< customControls:FloatTextBox PlaceholderText =" Sample Text" Text =" {Binding TestValue,Mode = TwoWay}" />
< TextBlock Text =" Float TextBox No X"
Margin =" 0,20,0,0" />
< customControls:FloatTextBox PlaceholderText =" Sample Text"
FloatTextMode =" NoX"
InputScopeNameValue =" Number" />
< Button Content =" To Password Overlay"
Click =" Button_Click"
Margin =" 0,20,0,0" />
< Button Content =" Check MyValue"
Click =" Button_Click_1" />
< / StackPanel>
< / Grid>
< / Page>

该页面背后的代码

 public密封的分部类MainPage:Page,INotifyPropertyChanged 
{
private string _testValue;
public string TestValue
{
get {return _testValue; }
设置
{
if(_testValue!= value)
{
_testValue = value;
NotifyPropertyChanged(nameof(TestValue));
}
}
}

public MainPage()
{
this.InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
TestValue =" Do Something Please" ;;
base.OnNavigatedTo(e);
}

private void Button_Click(object sender,RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PasswordOverlayPage));
}

private async void Button_Click_1(object sender,RoutedEventArgs e)
{
await new MessageDialog(TestValue).ShowAsync();
}

公共事件PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string info)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(info));
}
}

解决方案

Hello I have a UserControl which is just a textbox with some extra code to achieve the behavior I'd like in the templates.

My issue is that when I bind it to a property on a ViewModel, the textbox does not display the information.

The Codebehind for the UserControl

        /// <summary>
        /// Contains the Text for the Float TextBox
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
                NotifyPropertyChanged("Text");
            }
        }

        /// <summary>
        /// Dependency Property to hold the Text for the Float TextBox.
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(FloatTextBox), null);


The XAML file for the UserControl

        <TextBox x:Name="TextBox"
                 GotFocus="TextBox_GotFocus"
                 LostFocus="TextBox_LostFocus"
                 TextChanged="TextBox_TextChanged"
                 PointerEntered="TextBox_PointerEntered"
                 PointerExited="TextBox_PointerExited"
                 PointerPressed="TextBox_PointerPressed"
                 PointerReleased="TextBox_PointerReleased"
                 PlaceholderText="{Binding PlaceholderText}"
                 Header="{Binding PlaceholderText}"
                 TextWrapping="{Binding TextWrapping}"
                 Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                 IsReadOnly="{Binding IsReadOnly}"
                 BorderThickness="{Binding BorderThickness}"
                 BorderBrush="{Binding BorderBrush}"
                 AcceptsReturn="{Binding AcceptsReturn}"
                 MaxLength="{Binding MaxLength}"/>

The page I am testing the control on

<Page
    x:Class="CustomControlsTestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CustomControlsTestApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:customControls="using:CustomControlLibrary"
    mc:Ignorable="d">

    <Grid Background="AliceBlue">
        <StackPanel Orientation="Vertical">
            <TextBlock Text="PasswordBox"
                       Margin="0,20,0,0"/>
            <customControls:FloatPasswordBox PlaceholderHeaderText="Password"
                                             Password="Test"/>
            <TextBlock Text="Float TextBox"
                       Margin="0,20,0,0"/>
            <customControls:FloatTextBox PlaceholderText="Sample Text" Text="{Binding TestValue, Mode=TwoWay}"/>
            <TextBlock Text="Float TextBox No X"
                       Margin="0,20,0,0" />
            <customControls:FloatTextBox PlaceholderText="Sample Text"
                                         FloatTextMode="NoX"
                                         InputScopeNameValue="Number"/>
            <Button Content="To Password Overlay"
                    Click="Button_Click"
                    Margin="0,20,0,0" />
            <Button Content="Check MyValue"
                    Click="Button_Click_1" />
        </StackPanel>
    </Grid>
</Page>

And the code behind for that page

    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        private string _testValue;
        public string TestValue
        {
            get { return _testValue; }
            set
            {
                if (_testValue != value)
                {
                    _testValue = value;
                    NotifyPropertyChanged(nameof(TestValue));
                }
            }
        }

        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            TestValue = "Do Something Please";
            base.OnNavigatedTo(e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(PasswordOverlayPage));
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            await new MessageDialog(TestValue).ShowAsync();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }
    }
解决方案


这篇关于绑定不会更新用户控件[UWP] [XAML]上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:54