在没有任何理由的情况下,可以在任何地方使用而不是TextBox.Text。我需要的是在DataContext更改时获取文本框的更新 Text 属性。有一个DataGrid和它后面的另一个控件来显示有关行的详细信息。因此,当用户单击行的时,我将从网格行获取数据对象,并在此详细信息控件中显示其详细信息。详细信息控件仅包含propertyName和value控件。即文本块和文本框。文本块或文本框的任何属性都会在更改DataContext时更新,但文本框中的文本除外。我已经打破了我的头脑。 主要问题是我更改后文本框不会更新。如果将控件外部的文本框移动到详细信息控件(父级)
,则可以正常工作属性 - 值控制:
<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则不然。这是什么?
UPD: 的 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);
}
}
答案 0 :(得分:1)
猜猜:
1)该文本框具有textchanged事件处理程序集。检查该处理程序是否正常。尝试评论它,看看它是否有任何改变。即也许它会在模型的数据更新过程中被触发,也许它会引发异常并且同时中止绑定?
1a)在调试模式下运行VS并选中“输出”窗口。看看是否有任何报道如下:
非常重要,尤其是当您第一次尝试编辑文本框时它们出现
2)此外,每当绑定神奇地停止工作时,请务必检查绑定是否仍然存在。我看到你正在使用代码隐藏的控件直接访问(如textbox.Height += ..
)。这是打破绑定的简单方法。如果你曾经在任何地方运行过其中一行:
textBox.Text = ""
textBox.Text = "foo"
textBox.Text = john.name
textBox.Text += "."
这些可能很有可能在该文本框上的文本上取消设置绑定。我在你提供的代码中没有看到任何这样的行,但也许你在其他地方也有。
您可以通过运行来轻松检查绑定是否仍然无效:
object realvalue = textBox.ReadLocalValue(TextBox.TextProperty);
现在如果realvalue
为null,或者字符串,或者除了Binding对象之外的任何东西 - 这意味着某些东西访问了文本框并用具体的常量值替换了绑定,你需要找到并纠正它所以.Text没有被分配,而是改变了源对象(customdockpanel)的Value属性。