我在开源应用程序中有一个我想要更改的自定义控件。
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 }更多推荐
发布评论