如何将命令绑定到用户控件中数据模板中的复选框?
How do I bind a Command to a Checkbox in a Datatemplate in a user control?
我有一个用户控件,里面有一个 DataTemplate。在 DataTemplate 中有一个复选框,我想将命令绑定到该复选框。我已经创建了一个依赖项 属性 来绑定命令,但我无法找出正确的 XAML 语法来将命令从视图中的 XAML 绑定到用户控件中的复选框。然后在视图中的 DataTemplate 中使用用户控件本身。我正在寻找的是将视图模型中的命令绑定到此用户控件的最佳方法。怎么弄都绑定不成功
用户控制XAML
<UserControl
...
x:Name="orderTreeItemControl"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="LocationTemplate" x:DataType="model:Location">
<StackPanel Orientation="Horizontal">
<CheckBox Command="{}" IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{x:Bind LocationName}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
...
</StackPanel>
</DataTemplate>
...
</UserControl.Resources>
<Grid>
<ContentControl x:Name="MainContent"/>
</Grid>
</UserControl>
后面的用户控制代码:
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command1", typeof(ICommand), typeof(OrderTreeItemControl), new PropertyMetadata(null, new PropertyChangedCallback(Data_Changed)));
public ICommand Command1
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
查看XAML
<Page.Resources>
<DataTemplate x:Key="BaseTemplate" x:DataType="model:OpenOrdersBase">
<winui:TreeViewItem
ItemsSource="{x:Bind Children}"
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls1:OrderTreeItemControl Command1="{Binding DataContext.PickToggle, RelativeSource={RelativeSource Mode=Self} }" Data="{Binding}">
</controls1:OrderTreeItemControl>
</winui:TreeViewItem>
</DataTemplate>
更新:
我尝试了 Faywang 提出的解决方案,并尝试以两种不同的方式将 Command1 属性 绑定到视图。它仍然没有正确绑定命令。
查看 XAML 尝试 1:
<Page.Resources>
<DataTemplate x:Key="BaseTemplate" x:DataType="model:OpenOrdersBase">
<winui:TreeViewItem
ItemsSource="{x:Bind Children}"
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls1:OrderTreeItemControl Command1="{Binding DataContext.PickToggle, RelativeSource={RelativeSource Mode=Self} }" Data="{Binding}">
</controls1:OrderTreeItemControl>
</winui:TreeViewItem>
</DataTemplate>
查看 XAML 尝试 2(添加 x:name 到页面):
<Page.Resources>
<DataTemplate x:Key="BaseTemplate" x:DataType="model:OpenOrdersBase">
<winui:TreeViewItem
ItemsSource="{x:Bind Children}"
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls1:OrderTreeItemControl Command1="{Binding ElementName=PickSheetManager, Path=ViewModel.PickToggle}" Data="{Binding}">
</controls1:OrderTreeItemControl>
</winui:TreeViewItem>
</DataTemplate>
您可以使用 Binding.ElementName 属性 获取您的用户控件的名称以用作绑定的绑定源。然后您可以访问它的 Command1 属性 并将其与 CheckBox 绑定。例如:
.xaml:
<UserControl
...
x:Name="orderTreeItemControl"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="LocationTemplate" x:DataType="local:Location">
<StackPanel Orientation="Horizontal">
<CheckBox Command="{Binding ElementName=orderTreeItemControl,Path=Command1}" IsChecked="{Binding IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{Binding LocationName}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
......
</UserControl>
我有一个用户控件,里面有一个 DataTemplate。在 DataTemplate 中有一个复选框,我想将命令绑定到该复选框。我已经创建了一个依赖项 属性 来绑定命令,但我无法找出正确的 XAML 语法来将命令从视图中的 XAML 绑定到用户控件中的复选框。然后在视图中的 DataTemplate 中使用用户控件本身。我正在寻找的是将视图模型中的命令绑定到此用户控件的最佳方法。怎么弄都绑定不成功
用户控制XAML
<UserControl
...
x:Name="orderTreeItemControl"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="LocationTemplate" x:DataType="model:Location">
<StackPanel Orientation="Horizontal">
<CheckBox Command="{}" IsChecked="{x:Bind IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{x:Bind LocationName}" Margin="{StaticResource XXSmallTopRightBottomMargin}" />
...
</StackPanel>
</DataTemplate>
...
</UserControl.Resources>
<Grid>
<ContentControl x:Name="MainContent"/>
</Grid>
</UserControl>
后面的用户控制代码:
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command1", typeof(ICommand), typeof(OrderTreeItemControl), new PropertyMetadata(null, new PropertyChangedCallback(Data_Changed)));
public ICommand Command1
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
查看XAML
<Page.Resources>
<DataTemplate x:Key="BaseTemplate" x:DataType="model:OpenOrdersBase">
<winui:TreeViewItem
ItemsSource="{x:Bind Children}"
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls1:OrderTreeItemControl Command1="{Binding DataContext.PickToggle, RelativeSource={RelativeSource Mode=Self} }" Data="{Binding}">
</controls1:OrderTreeItemControl>
</winui:TreeViewItem>
</DataTemplate>
更新:
我尝试了 Faywang 提出的解决方案,并尝试以两种不同的方式将 Command1 属性 绑定到视图。它仍然没有正确绑定命令。
查看 XAML 尝试 1:
<Page.Resources>
<DataTemplate x:Key="BaseTemplate" x:DataType="model:OpenOrdersBase">
<winui:TreeViewItem
ItemsSource="{x:Bind Children}"
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls1:OrderTreeItemControl Command1="{Binding DataContext.PickToggle, RelativeSource={RelativeSource Mode=Self} }" Data="{Binding}">
</controls1:OrderTreeItemControl>
</winui:TreeViewItem>
</DataTemplate>
查看 XAML 尝试 2(添加 x:name 到页面):
<Page.Resources>
<DataTemplate x:Key="BaseTemplate" x:DataType="model:OpenOrdersBase">
<winui:TreeViewItem
ItemsSource="{x:Bind Children}"
IsExpanded="{x:Bind IsExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls1:OrderTreeItemControl Command1="{Binding ElementName=PickSheetManager, Path=ViewModel.PickToggle}" Data="{Binding}">
</controls1:OrderTreeItemControl>
</winui:TreeViewItem>
</DataTemplate>
您可以使用 Binding.ElementName 属性 获取您的用户控件的名称以用作绑定的绑定源。然后您可以访问它的 Command1 属性 并将其与 CheckBox 绑定。例如:
.xaml:
<UserControl
...
x:Name="orderTreeItemControl"
mc:Ignorable="d">
<UserControl.Resources>
<DataTemplate x:Name="LocationTemplate" x:DataType="local:Location">
<StackPanel Orientation="Horizontal">
<CheckBox Command="{Binding ElementName=orderTreeItemControl,Path=Command1}" IsChecked="{Binding IsSelected, Mode=TwoWay}" Padding="5" MinWidth="0" />
<TextBlock Text="{Binding LocationName}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
......
</UserControl>