1 回答

TA貢獻1773條經驗 獲得超3個贊
我簡化了您的用戶控件,使其更具可讀性,并且只關注工作的外部命令。我修改命令的綁定。在列表中,您獲得了項目的本地數據上下文,但需要將命令綁定到外部數據上下文。
<ComboBox ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}">
<ComboBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding .}"
Click="CheckBox_Click"
Command="{Binding ElementName=UserControl,Path=YourCommand}">
</CheckBox>
</HierarchicalDataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在 UserControl1.cs 中我得到:
public ICommand YourCommand
{
get { return (ICommand)GetValue(YourCommandProperty); }
set { SetValue(YourCommandProperty, value); }
}
// Using a DependencyProperty as the backing store for YourCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty YourCommandProperty =
DependencyProperty.Register("YourCommand", typeof(ICommand), typeof(UserControl1), new PropertyMetadata(null));
我測試過,它對我有用。
- 1 回答
- 0 關注
- 91 瀏覽
添加回答
舉報