【问题标题】:WPF TextBox.Text doesn't update in another controlWPF TextBox.Text 不会在另一个控件中更新
【发布时间】:2016-10-21 17:36:11
【问题描述】:

这是一个简单的东西,可以在任何地方而不是 TextBox.Text 没有任何理由地工作。我需要的只是在 DataContext 更改时获取文本框的更新 Text 属性。后面有一个 DataGrid 和另一个控件,用于显示有关行的详细信息。因此,当用户单击行时,我将从网格的行中获取数据对象并在此详细信息控件中显示其详细信息。 Details 控件只包含 propertyName 和 value 控件。 IE。文本块和文本框。文本块或文本框的任何属性都会在更改 DataContext 时更新,但文本框中的 Text 除外。我已经打破了我的头。 主要问题是文本框在我更改后不会更新。如果将文本框外部控件移动到详细信息控件(父级),它工作正常

属性值控件:

    <DockPanel x:Class="UserControls.PropertySectionControl"
                 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"              
                 mc:Ignorable="d"       
                 x:Name="_propertyDockPanel"
                 HorizontalAlignment="Stretch"  Margin="0 5 0 0"
                 d:DesignHeight="300" d:DesignWidth="300">

        <TextBlock  Text="{Binding Path=Property, ElementName=_propertyDockPanel}" TextWrapping="Wrap"/>
        <TextBox              
  Text="{Binding Path=Value, ElementName=_propertyDockPanel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                 IsReadOnly="{Binding Path=IsReadOnly, ElementName=_propertyDockPanel}"             
                 TextAlignment="Right" Foreground="Gray" TextWrapping="Wrap" Width="120" Height="20" HorizontalAlignment="Right" Margin="0 0 15 0"
                 ToolTip="{Binding Value, ElementName=_propertyDockPanel}" Template="{StaticResource _inspectorValueTemplate}"
                 LostFocus="_textBoxValue_LostFocus" GotFocus="_textBoxValue_LostFocus" KeyDown="_textBoxValue_KeyDown" TextChanged="_textBoxValue_TextChanged"         
             />
    </DockPanel>

下面是它在细节控制中的使用方式:

<userControls:PropertySectionControl Property="Total" Value="{Binding OrderCharges.Total}" IsReadOnly="True"/>

如您所见,在 PropertySectionControl 中还绑定了 ToolTip 到 Value,它适用于 DataContext 已更改!但是对于 TextBox.Text 不是。这是什么?

更新: PropertySectionControl.cs

public partial class PropertySectionControl : DockPanel
    {
        public string Property
        {
            get { return (string)GetValue(PropertyProperty); }
            set { SetValue(PropertyProperty, value); }
        }

        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }


        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(PropertySectionControl), new PropertyMetadata(""));


        public static readonly DependencyProperty PropertyProperty =
            DependencyProperty.Register("Property", typeof(string), typeof(PropertySectionControl), new PropertyMetadata(""));


        public static readonly DependencyProperty IsReadOnlyProperty =
                    DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(PropertySectionControl), new PropertyMetadata(false));


        public PropertySectionControl()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Static event handler is for use in Inspector control as well
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void TextBox_LostOrGotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox.IsFocused)
            {
                textBox.TextAlignment = TextAlignment.Left;
                textBox.SelectAll();
            }
            else
            {
                textBox.TextAlignment = TextAlignment.Right;                
            }
        }

        public static void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            var textBox = sender as TextBox;
            if (e.Key == Key.Enter)
            {
                textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }

            else if (e.Key == Key.Escape)
            {
                int i = 0;
                do
                {
                    textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));

                } while (!(Keyboard.FocusedElement is TextBoxBase) && ++i < 5);      // prevent infinite loop

                Keyboard.ClearFocus();
            }
        }

        public static void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = sender as TextBox;
            textBox.Height = 20;
            for (int i = 2; i <= textBox.LineCount; i++)
                textBox.Height += 14;
        }

        private void _textBoxValue_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox_LostOrGotFocus(sender, e);
        }

        private void _textBoxValue_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox_KeyDown(sender, e);
        }

        private void _textBoxValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox_TextChanged(sender, e);
        }
    }

【问题讨论】:

  • 我很确定,Dockpanel 没有属性值
  • 感谢您的快速反馈,但确实如此。如前所述,将 ToolTip 绑定到 Value 是可以的
  • 试试get { return GetValue(PropertyProperty).ToString(); }
  • 您的 TextBox 绑定中是否应该有 Mode=TwoWay?如果它只是一个工具提示,您不应该将值放回 DataGrid 中吗?
  • 感谢您的提示。我已经更新了这个问题。主要问题是文本框在我编辑后不会进一步更新。所以它在第一次编辑之前更新好了。

标签: c# wpf xaml


【解决方案1】:

只是猜测:

1) 该文本框有一个 textchanged 事件处理程序集。检查该处理程序是否工作正常。尝试将其注释掉,看看它是否会改变任何东西。 IE。也许它在模型的数据更新期间被触发,也许它抛出一个异常并且绑定在此期间被中止?

1a) 在 VS 中以调试模式运行并检查“输出”窗口。看看是否有类似的报告:

  • 第一次机会例外
  • 绑定错误

尤其是当您第一次尝试编辑文本框时它们出现时很重要

2) 此外,每当绑定神奇地停止工作时,请务必检查绑定是否仍然存在。我看到您正在使用从代码隐藏(如textbox.Height += ..)直接访问控件。这是打破绑定的简单方法。如果你曾经在任何地方运行过这些行之一:

textBox.Text = ""
textBox.Text = "foo"
textBox.Text = john.name
textBox.Text += "."

这些可能很有可能会取消您对该文本框上文本的绑定。我在您提供的代码中没有看到任何这样的行,但也许您在其他地方有。

您可以通过运行轻松检查绑定是否仍然无效:

object realvalue = textBox.ReadLocalValue(TextBox.TextProperty);

现在,如果realvalue 为空、字符串或 Binding 对象以外的任何内容 - 这意味着有东西访问了文本框并将绑定替换为具体的常量值,您需要找到并更正它,以便.Text 未分配,而是更改了源对象(customdockpanel)的 Value 属性。

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多