扩展UserControl WPF的自定义控件中的TextChanged或ContentChanged事件(TextChanged or ContentChanged event in a custo

编程入门 行业动态 更新时间:2024-10-28 12:30:47
扩展UserControl WPF的自定义控件中的TextChanged或ContentChanged事件(TextChanged or ContentChanged event in a custom control extending UserControl WPF)

我在开源应用程序中有一个我想要更改的自定义控件。

XAML看起来像这样:

<controls:Scratchpad Grid.Row="1" Grid.Column="2" Text="{Binding DataContext.KeyboardOutputService.Text, RelativeSource={RelativeSource AncestorType=controls:KeyboardHost}, Mode=OneWay}"/>

Scratchpad控件的代码隐藏如下所示:

public class Scratchpad : UserControl { public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof (string), typeof (Scratchpad), new PropertyMetadata(default(string))); public string Text { get { return (string) GetValue(TextProperty); } set { SetValue(TextProperty, value); } } }

我想在每次UserControl中的文本更改时触发事件处理程序。 但是,我没有可以在XAML中使用的TextChanged事件。

我的计划是做这样的事情:

<controls:Scratchpad Grid.Row="1" Grid.Column="2" Text="{Binding DataContext.KeyboardOutputService.Text, RelativeSource={RelativeSource AncestorType=controls:KeyboardHost}, Mode=OneWay}" textChanged="EventHandler"/>

但是,此自定义控件中不存在“textChanged”事件。

如您所见,ScratchPad扩展了UserControl。 UserControl还扩展了ContentControl,这就是为什么我认为可以将文本放在这个控件中,它们可能是我不知道的“ContentChanged”事件。

最好,彼得。

I have a custom control in an open source application that I want to change.

The XAML looks like this:

<controls:Scratchpad Grid.Row="1" Grid.Column="2" Text="{Binding DataContext.KeyboardOutputService.Text, RelativeSource={RelativeSource AncestorType=controls:KeyboardHost}, Mode=OneWay}"/>

The codebehind for the Scratchpad control looks like this:

public class Scratchpad : UserControl { public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof (string), typeof (Scratchpad), new PropertyMetadata(default(string))); public string Text { get { return (string) GetValue(TextProperty); } set { SetValue(TextProperty, value); } } }

I want to trigger an event handler each time the text changes in the UserControl. However there is no TextChanged event that I can use in the XAML.

My plan was to do something like this:

<controls:Scratchpad Grid.Row="1" Grid.Column="2" Text="{Binding DataContext.KeyboardOutputService.Text, RelativeSource={RelativeSource AncestorType=controls:KeyboardHost}, Mode=OneWay}" textChanged="EventHandler"/>

However the "textChanged" event does not exist in this custom control.

As you can see, ScratchPad extends UserControl. UserControl also extends ContentControl, and that's why I think it is possible to put text in this control, their might be a "ContentChanged" event that I don't know about.

Best, Peter.

最满意答案

两种选择:

(MVVM方式)如果更改是为了反映域模型中的某些内容,则此更改可能最适合在viewmodel中处理

(控制方式)您是否考虑过在DependencyProperty中添加更改的处理程序?

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(ScratchPad), new PropertyMetadata(null, OnTextChanged)); private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Handle change here }

Two options:

(MVVM way) If the change is to reflect something in the domain model, perhaps this change is best suited for handling in your viewmodel

(Control way) Have you considered putting a changed handler in your DependencyProperty?

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(ScratchPad), new PropertyMetadata(null, OnTextChanged)); private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // Handle change here }

更多推荐

本文发布于:2023-08-04 09:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   事件   TextChanged   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!