数据绑定不适用于用户控件
本文关键字:控件 用户 适用于 不适用 数据绑定 | 更新日期: 2023-09-27 18:31:16
我正在尝试实现树节点的就地编辑。我离得很近,但没有雪茄。所有绑定似乎都是正确的。应用程序运行良好,没有输出消息,树视图已填充。我能够选择一个节点,按 F2,我可以编辑节点,但一旦我按 Enter 文本就会恢复到之前的状态,就好像我没有进行编辑一样。如果我在应用程序中的其他地方编辑 Name 属性,则更改将反映在 EditableTextBlock 和文本框中。为什么我不能使更改持久化?就好像绑定只以一种方式工作一样。
这是用户控件的 xaml:
<UserControl x:Class="WPFApplication.EditableTextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFApplication"
x:Name="MyUserControl">
<UserControl.Resources>
<DataTemplate x:Key="EditModeTemplate">
<TextBox KeyDown="TextBox_KeyDown" Loaded="TextBox_Loaded" LostFocus="TextBox_LostFocus"
Text="{Binding Path=Text, ElementName=MyUserControl, Mode=TwoWay}"
Margin="0" BorderThickness="1" />
</DataTemplate>
<DataTemplate x:Key="DisplayModeTemplate">
<TextBlock Text="{Binding ElementName=MyUserControl, Path=Text, Mode=TwoWay}"/>
</DataTemplate>
<Style TargetType="{x:Type local:EditableTextBlock}">
<Style.Triggers>
<Trigger Property="IsInEditMode" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource EditModeTemplate}" />
</Trigger>
<Trigger Property="IsInEditMode" Value="False">
<Setter Property="ContentTemplate" Value="{StaticResource DisplayModeTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
和背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WPFApplication
{
public partial class EditableTextBlock : UserControl
{
#region Constructor
public EditableTextBlock()
{
InitializeComponent();
base.Focusable = true;
base.FocusVisualStyle = null;
}
#endregion Constructor
#region Member Variables
// We keep the old text when we go into editmode
// in case the user aborts with the escape key
private string oldText;
#endregion Member Variables
#region Properties
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(EditableTextBlock),
new PropertyMetadata(""));
public bool IsEditable
{
get { return (bool)GetValue(IsEditableProperty); }
set { SetValue(IsEditableProperty, value); }
}
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.Register(
"IsEditable",
typeof(bool),
typeof(EditableTextBlock),
new PropertyMetadata(true));
public bool IsInEditMode
{
get
{
if (IsEditable)
return (bool)GetValue(IsInEditModeProperty);
else
return false;
}
set
{
if (IsEditable)
{
if (value) oldText = Text;
SetValue(IsInEditModeProperty, value);
}
}
}
public static readonly DependencyProperty IsInEditModeProperty =
DependencyProperty.Register(
"IsInEditMode",
typeof(bool),
typeof(EditableTextBlock),
new PropertyMetadata(false));
public string TextFormat
{
get { return (string)GetValue(TextFormatProperty); }
set
{
if (value == "") value = "{0}";
SetValue(TextFormatProperty, value);
}
}
public static readonly DependencyProperty TextFormatProperty =
DependencyProperty.Register(
"TextFormat",
typeof(string),
typeof(EditableTextBlock),
new PropertyMetadata("{0}"));
public string FormattedText
{
get { return String.Format(TextFormat, Text); }
}
#endregion Properties
#region Event Handlers
// Invoked when we enter edit mode.
void TextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox txt = sender as TextBox;
// Give the TextBox input focus
txt.Focus();
txt.SelectAll();
}
// Invoked when we exit edit mode.
void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
this.IsInEditMode = false;
}
// Invoked when the user edits the annotation.
void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.IsInEditMode = false;
e.Handled = true;
}
else if (e.Key == Key.Escape)
{
this.IsInEditMode = false;
Text = oldText;
e.Handled = true;
}
}
#endregion Event Handlers
}
}
然后,树视图使用以下数据模板:
<HierarchicalDataTemplate DataType="{x:Type localvm:TreeViewItemViewModel}" ItemsSource="{Binding Children}">
<StackPanel>
<local:EditableTextBlock Text="{Binding Name}"/>
</StackPanel>
好的,
我尝试了一下,问题是从未设置 Text 属性,因为您的文本框从未失去焦点(似乎)。
因此,我更改了文本框的文本绑定以添加 UpdateSourceTrigger=PropertyChanged 和 RelativeSource 而不是 ElementName:
Text="{Binding Path=Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:EditableTextBlock}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
它奏效了