C# 利用UserControl中的重写更改触发器的属性
我已经创建了一个实现ICommandSource的TextBox实例,我想通过DataContext控制IsEnabled属性。我的代码的这一部分可以工作,除此之外,我希望通过相同的方法或扩展IsEnabled属性来控制Text属性 基本上,当文本框从IsEnabled=“False”转换为IsEnabled=“True”时,我希望将文本字段重置为空字符串,或者最好为null 我尝试过几种方法,但都没有成功 尝试1C# 利用UserControl中的重写更改触发器的属性,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我已经创建了一个实现ICommandSource的TextBox实例,我想通过DataContext控制IsEnabled属性。我的代码的这一部分可以工作,除此之外,我希望通过相同的方法或扩展IsEnabled属性来控制Text属性 基本上,当文本框从IsEnabled=“False”转换为IsEnabled=“True”时,我希望将文本字段重置为空字符串,或者最好为null 我尝试过几种方法,但都没有成功 尝试1 <ctrl:CommandTextBox x:Name="txtSeria
<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>
DecoderTextBox.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;
}
}
}
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);
}
}
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);
}
是否有特定于我的OnKeyDown实现的东西破坏了此触发器?存在一个与本地设置DependencyProperty值相关的问题。似乎必须使用SetCurrentValue来维护绑定 DecoderTextBox.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;
}
}
}
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);
}
}
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);
}
一个问题,你的
CommandTextBox
是否碰巧位于模板内部?@XAMlMAX不,它位于作为UserControl一部分的网格内部。好的,只要它所属的可视化树的任何部分不是模板,就应该应用样式。您在输出窗口中得到了什么?它找不到属性吗?@XAMlMAX我在输出窗口中没有看到任何异常情况,我已经做了一些额外的故障排除,如果通过按钮执行相同的命令,文本确实会按预期更新。通过文本框使用命令似乎会中断样式?我不知道该怎么办?看起来你的代码可能导致了无效的结果,看看下面的代码,好吗