【发布时间】:2016-08-21 14:22:42
【问题描述】:
您好,我正在尝试使用删除按钮创建自定义 TabItem,我想将我的 viewmodel 命令绑定到我的自定义依赖属性“DeleteCommandProperty”。谁能告诉我我做错了什么?
我的自定义 TabControl:
/// <summary>
/// TabControl withCustom TabItem
/// </summary>
public class MyTabControl:TabControl
{
/// <summary>
/// TabItem override
/// </summary>
/// <returns></returns>
protected override DependencyObject GetContainerForItemOverride()
{
return new MyTabItem();
}
}
我的自定义 TabItem 类:
/// <summary>
/// Custom TabItem
/// </summary>
public class MyTabItem:TabItem
{
/// <summary>
/// Delete Command
/// </summary>
public static DependencyProperty DeleteCommandProperty = DependencyProperty.Register(
"DeleteCommand",typeof(ICommand),typeof(MyTabItem));
/// <summary>
/// Delete
/// </summary>
public ICommand DeleteCommand
{
get { return (ICommand)GetValue(DeleteCommandProperty); }
set { SetValue(DeleteCommandProperty, value); }
}
}
当我像这样直接绑定 DeleteCommand 时,我的 ViewModel 中的命令被执行
<customControls:MyTabControl>
<customControls:MyTabItem Header="Test" DeleteCommand="{Binding DeleteStudiengangCommand}" Template="{DynamicResource MyTabItemControlTemplate}"/>
</customControls:MyTabControl>
当尝试通过这样的样式绑定 deleteCommand 但它不起作用时:
<Style TargetType="customControls:MyTabItem">
<Setter Property="Template" Value="{DynamicResource MyTabItemControlTemplate}"/>
<Setter Property="DeleteCommand" Value="{Binding MyDeleteCommand}"/>
</Style>
<customControls:MyTabControl ItemsSource="{Binding MyList}" SelectedItem="{Binding SelectedItem}" SelectedIndex="0">
<customControls:MyTabControl.ContentTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Value}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</customControls:MyTabControl.ContentTemplate>
</customControls:MyTabControl>
【问题讨论】:
-
考虑将其减少为MVCE。例如,不要尝试以你的风格绑定
DeleteCommand,除非它对于显示你的问题是必不可少的......相反,在你的构造函数中分配一个静态命令 -
我看到你以某种方式编辑了你的问题,但它远非最小(你使用了许多与你的问题无关的模板),远非 verifiable(我用你的基于样式的命令设置器构建了一个小例子,它工作得很好),远非完整(
customControls:MyTabControl的代码在哪里?另外,请不要强迫我使用 Blend)并且您并没有真正解释与预期行为相比观察到的行为是什么,因此也没有给出 示例。 -
我添加了我的 TabControl 类并尝试解释什么不起作用希望有人能提供帮助
标签: c# wpf mvvm dependency-properties tabitem