我在将视图中包含的用户控件中绑定命令时遇到问题。
UserControl.xaml
<DataTemplate x:Key="SoldItemDataTemplate" x:DataType="data:SoldItem">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
...
<TextBox Grid.Column="2" Text="{x:Bind q}">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="TextChanged">
<Core:InvokeCommandAction Command="{Binding Path=DataContext.QChanged, ElementName=SoldRows}" CommandParameter="{Binding Mode=TwoWay}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBox>
</Grid>
</DataTemplate>
...
<ListView x:Name="SoldRows"
ItemsSource="{Binding SoldItemsList , Mode=TwoWay}"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource SoldItemDataTemplate}">
...
MainPage.xaml
<Page.DataContext>
<vm:MainPageViewModel x:Name="ViewModel"></vm:MainPageViewModel>
</Page.DataContext>
...
<controls:UserControl Grid.Row="1"></controls:UserControl>
...
MainPageViewModel.cs
...
private DelegateCommand<object> _QSoldChanged;
...
public DelegateCommand<object> QSoldChanged
{
get
{
if (_QSoldChanged == null)
{
_QSoldChanged = new DelegateCommand<object>((object par) =>
{
...
});
}
return _QSoldChanged;
}
}
因此,我想在TextChanged事件上调用QSoldChanged,但是什么也没有发生。我想念什么?在这种情况下,这是设置命令的正确方法吗?如果您需要更多,请问!提前致谢
问题在于文本框的x:Bind。它的模式是一种方式。因此,文本更改不会反映在视图模型上。将其更改为绑定。这也可能引起问题
{x:Bind}不使用DataContext作为默认源,而是使用页面或用户控件本身。因此,它将在页面或用户控件的代码隐藏中查找属性,字段和方法。要将视图模型公开给{x:Bind},通常需要在页面或用户控件的后面的代码中添加新的字段或属性。参考此链接x:Bind
希望它能工作
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句