我正在使用MvmmCross v3框架和Windows.UI.Interactivity开发WinRT。
我想在TextChanged事件上使用带有EventTrigger的TextBox来启动命令。而且,我想在CommandParameter中使用textBox的文本。
所以我有这个代码
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding UpdateText}" CommandParameter="{Binding Text}"/>
</i:EventTrigger>
public ICommand UpdateText
{
get
{
return new MvxCommand<object>((textSearch) =>
{
// code...
});
}
}
但我的textSearch参数等于“{Windows.UI.Xaml.Controls.TextChangedEventArgs}”,所有这些属性都为NULL。
我也尝试在绑定中明确声明我的ElementName,如
CommandParameter="{Binding Path=Text, ElementName=tes}
但它也失败了。
由于
答案 0 :(得分:7)
你真的需要处理TextChanged
事件吗?只需绑定到Text
属性即可通知您更改:
<TextBox Text="{Binding TextValue, Mode=TwoWay}" />
然后在视图模型中:
private string _textValue;
public string TextValue
{
get
{
return _textValue;
}
set
{
if (_textValue == value)
return;
_textValue = value;
OnTextChanged(value); // react to the changed value
}
}
修改强>
如果您想从Text
内部获取Command
值,则需要注意两件事:
首先,您需要修复CommandParameter
绑定。通过使用{Binding Text}
,您实际上尝试绑定到视图模型中的属性,即首先需要将TextBox.Text
属性绑定到同一视图模型属性。正如您在评论中所说,这对您没有好处,因为您需要每次更改的信息,而不仅仅是丢失焦点,因此您获得的价值不是最新的。
因此,正确的方法是您的第二次尝试,即使用TextBox
语法直接绑定到ElementName
。不幸的是,触发器不是可视化树的一部分,因此它们无法访问XAML名称范围(您可以在this blog post中阅读更多相关内容)。您可以使用MVVMHelpers.Metro package中的NameScopeBinding
来解决这个问题:
<Behaviors:NameScopeBinding x:Key="MyTextBox"
Source="{Binding ElementName=MyTextBox}" />
现在您可以使ElementName
绑定工作:
<i:InvokeCommandAction Command="{Binding UpdateText}"
CommandParameter="{Binding Source.Text, Source={StaticResource MyTextBox}}"/>
您仍然遇到第二次问题。您绑定的Text
值仅在丢失焦点时更新,因此在处理TextChanged
事件时您无法获得正确的值。解决方案是绑定到TextBox
本身:
<i:InvokeCommandAction Command="{Binding UpdateText}"
CommandParameter="{Binding Source, Source={StaticResource MyTextBox}}"/>
然后在命令中直接从Text
:
TextBox
属性
private void OnUpdateText(object parameter)
{
var value = ((TextBox) parameter).Text;
// process the Text value as you need to.
}
编辑2:
要使上述代码适用于PCL中的视图模型,您可以采取几种方法。
最简单的黑客将使用反射。由于它在PCL中可用,您可以访问Text
属性并读取其值:
private void OnUpdateText(object parameter)
{
var textProperty = textSearch.GetType().GetProperty("Text");
var value = textProperty.GetValue(parameter, null) as string;
// process the Text value as you need to.
}
更清洁的解决方案是将WinRT特定代码放入通过接口抽象的单独程序集中。您首先要在PCL库中创建一个接口:
public interface IUiService
{
string GetTextBoxText(object textBox);
}
修改视图模型以在构造函数中接受此接口:
public ViewModel(IUiService uiService)
{
_uiService = uiService;
}
在命令处理程序中,您将使用界面中的方法:
private void OnUpdateText(object parameter)
{
var value = _uiService.GetTextBoxText(parameter);
// process the Text value as you need to.
}
您将在WinRT库中实现该接口:
public class UiService : IUiService
{
public string GetTextBoxText(object textBox)
{
var typedTextBox = textBox as TextBox;
return typedTextBox.text;
}
}
在应用程序中,您引用此库并将实现传递给视图模型:
var viewModel = new ViewModel(new UiService);
我的最喜欢的方法是不同的:我创建了一个Behavior
公开Text
属性,每次触发TextChanged
事件时都会自动更新:
public class TextChangedBehavior : Behavior<TextBox>
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(TextChangedBehavior),
new PropertyMetadata(null));
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.TextChanged += OnTextChanged;
Text = AssociatedObject.Text;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.TextChanged -= OnTextChanged;
}
private void OnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
Text = AssociatedObject.Text;
}
}
现在我可以将TextValue
属性绑定到此行为,并对此属性设置器中的更改作出反应,如此长答案的开头所建议的那样:
<TextBox>
<i:Interaction.Behaviors>
<b:TextChangedBehavior Text="{Binding TextValue, Mode=TwoWay}" />
</i:Interaction.Behaviors>
</TextBox>