简体   繁体   English

利用UserControl中的替代来更改属性中断触发器

[英]Utilizing Override in UserControl to Change Property Breaks Trigger

I have created an instance of a TextBox that implements ICommandSource, I would like to control the IsEnabled property via the DataContext. 我创建了一个实现ICommandSource的TextBox实例,我想通过DataContext控制IsEnabled属性。 This portion of my code works, on top of this I would like to control the Text property via this same method or by extension the IsEnabled property. 我的代码的这一部分有效,最重要的是,我想通过相同的方法或通过扩展IsEnabled属性来控制Text属性。

Basically when the TextBox transitions from IsEnabled="False" to IsEnabled="True" I would like to reset the Text field to an empty string or preferably null. 基本上,当TextBox从IsEnabled =“ False”转换为IsEnabled =“ True”时,我想将Text字段重置为空字符串,最好重置为null。

I have attempted to do this in a handful of ways without success. 我尝试了几种方法而没有成功。

Attempt 1 尝试1

<ctrl:CommandTextBox x:Name="txtSerialNumber"
                     Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                     CommandParameter="{Binding Text, RelativeSource={RelativeSource Self}}" DecoderPrefix="S">
    <ctrl:CommandTextBox.Style>
        <Style TargetType="{x:Type ctrl:CommandTextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="True" />
                    <Setter Property="Text" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="Text" Value="{Binding SerialNumber, Mode=OneWay}" />
        </Style>
    </ctrl:CommandTextBox.Style>
</ctrl:CommandTextBox>

This does work but only when the CommandParameter does not need to be "Decoded". 这确实有效,但是仅当CommandParameter不需要“解码”时才有效。 It seems as though when my text property is changed via the override it breaks the trigger until the application is restarted. 好像通过覆盖更改了我的text属性时,它会中断触发器,直到重新启动应用程序为止。

CommandTextBox.cs CommandTextBox.cs

public class CommandTextBox : DecoderTextBox, ICommandSource
{
    // Additional Fields, Properties, and Methods removed for the sake of brevity.

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);

        if (e.Key == Key.Enter && Command != null)
        {
            RoutedCommand command = Command as RoutedCommand;

            if (command != null)
                command.Execute(CommandParameter, CommandTarget);
            else
                Command.Execute(CommandParameter);

            if (CommandResetsText)
                this.Text = String.Empty;

            e.Handled = true;
        }
    }
}

DecoderTextBox.cs DecoderTextBox.cs

public class DecoderTextBox : TextBox
{
    public static DependencyProperty DecoderPrefixProperty = DependencyProperty.Register("DecoderPrefix", typeof(string), typeof(DecoderTextBox), new PropertyMetadata(String.Empty));

    public string DecoderPrefix
    {
        get { return (string)GetValue(DecoderPrefixProperty); }
        set { SetValue(DecoderPrefixProperty, value); }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            string text = this.Text;

            // If the if statement returns true the trigger will break.
            if (text.Substring(0, Math.Min(DecoderPrefix.Length, text.Length)) == DecoderPrefix)
                this.Text = text.Remove(0, DecoderPrefix.Length);
        }

        base.OnKeyDown(e);
    }
}

Is there something specific to my implementation of OnKeyDown that is breaking this trigger? 我的OnKeyDown实现是否有特定的东西打破了此触发器?

There is an issue related to setting the value of a DependencyProperty locally. 存在与在本地设置DependencyProperty的值有关的问题。 It appears as though you have to use SetCurrentValue to maintain the binding. 似乎您必须使用SetCurrentValue来维护绑定。

DecoderTextBox.cs DecoderTextBox.cs

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        if (Text.StartsWith(DecoderPrefix))
            SetCurrentValue(TextProperty, Text.Remove(0, DecoderPrefix.Length));
    }

    base.OnPreviewKeyDown(e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM