[英]WPF-Custom TabItem with Delete Button Binding Issue
嗨,我正在尝试使用Delete Button创建一个自定义TabItem,我想将viewmodel命令绑定到我的自定义Dependency属性'DeleteCommandProperty'。 有人可以告诉我我在做什么错吗?
我的自定义TabControl:
/// <summary>
/// TabControl withCustom TabItem
/// </summary>
public class MyTabControl:TabControl
{
/// <summary>
/// TabItem override
/// </summary>
/// <returns></returns>
protected override DependencyObject GetContainerForItemOverride()
{
return new MyTabItem();
}
}
我的Custom 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中的Command
<customControls:MyTabControl>
<customControls:MyTabItem Header="Test" DeleteCommand="{Binding DeleteStudiengangCommand}" Template="{DynamicResource MyTabItemControlTemplate}"/>
</customControls:MyTabControl>
bu当尝试通过这样的样式绑定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>
TL; DR
我怀疑你DataContext
包含您的当前项目MyList
(不管它是什么),所以风格二传手不能访问MyDeleteCommand
你计划财产。 查看Visual Studio调试器日志,查看是否在其中记录了绑定异常。
从工作代码演变到碰巧发现可能的问题解释的示例。
似乎您在简化示例方面有些困难,因此根据您提供的信息,我唯一能为您提供的是一个使用Style
and TemplateBinding
方法的小型工作示例,您可以以此为基础来确定您的实际问题。 编辑:最后的解释实际上可能是您的问题的答案。
请注意,您可能必须更改名称空间以与您的项目设置相匹配。
MainWindow.xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded">
<Window.Resources>
<!-- Header template -->
<ControlTemplate x:Key="MyTabItemControlTemplate" TargetType="{x:Type local:MyTabItem}">
<!-- Some text and the command button with template binding -->
<StackPanel Orientation="Horizontal" Background="Aquamarine" Margin="3">
<ContentPresenter Content="{TemplateBinding Header}" VerticalAlignment="Center" Margin="2"/>
<Button Content="Delete" Command="{TemplateBinding DeleteCommand}" Margin="2"/>
</StackPanel>
</ControlTemplate>
<!-- Setting the control template and assigning the command implementation -->
<Style TargetType="{x:Type local:MyTabItem}">
<Setter Property="Template" Value="{DynamicResource MyTabItemControlTemplate}"/>
<Setter Property="DeleteCommand" Value="{Binding MyDeleteCommand}"/>
<Setter Property="Header" Value="Default Header Text"/>
</Style>
</Window.Resources>
<Grid>
<local:MyTabControl ItemsSource="{Binding MyTabItemList}"/>
</Grid>
</Window>
MainWindow.xaml.cs
/// <summary>
/// TabControl withCustom TabItem
/// </summary>
public class MyTabControl : TabControl
{
/// <summary>
/// TabItem override
/// </summary>
protected override DependencyObject GetContainerForItemOverride()
{
return new MyTabItem();
}
}
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); }
}
}
public class MyCommand : ICommand
{
public void Execute(object parameter)
{
MessageBox.Show("Hello WPF", "Message");
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
}
public class MyContext
{
public ICommand MyDeleteCommand { get; set; }
public List<object> MyTabItemList { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var list = new List<object>();
list.Add(new TextBlock() { Text = "Test 1" });
list.Add(new MyTabItem() { Content = "Test Content 2", Header = "Test Header 2" });
list.Add(new TabItem() { Content = "Test Content 3", Header = "Test Header 3" });
this.DataContext = new MyContext()
{
MyTabItemList = list,
MyDeleteCommand = new MyCommand()
};
}
}
示例摘要:您将看到三个不同的选项卡,每个选项卡都有其独特的构建和外观:
GetContainerForItemOverride
方法创建的, Header
属性的样式设置Header
指定出现在标题中的文本。 MyDeleteCommand
绑定不起作用! MyTabItem
提供,带有标题和内容值。 Header
属性的样式设置器不适用,因为该属性是显式设置的。 DeleteCommand
属性的样式设置器从MyContext
绑定到MyDeleteCommand
属性。 TabItem
提供,并且由于没有MyTabControl.IsItemItsOwnContainerOverride
覆盖,因此可以将其作为MyTabControl
的控制项接受。 整个MyTabItem
模板不适用。 Visual Studio内部的调试输出提示有关第一个选项卡项的潜在问题:
System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ TextBlock”(名称=“)”上找不到“ MyDeleteCommand”属性。 BindingExpression:Path = MyDeleteCommand; DataItem ='TextBlock'(Name =''); 目标元素是'MyTabItem'(Name =''); 目标属性是“ DeleteCommand”(类型“ ICommand”)
原因是,在这种情况下,当前选项卡项的内容成为新的本地DataContext
,这与第二个选项卡项不同,在第二个选项卡项中,该项本身被接受为容器。
一种解决方案是确保在命令绑定上使用propper上下文:
假设您给一些合适的父元素一个名称x:Name="_this"
,那么您可以访问父DataContext
。
<Setter Property="DeleteCommand" Value="{Binding DataContext.MyDeleteCommand,ElementName=_this}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.