我有一个选项卡式部分,我试图连接关闭和动态打开新选项卡的命令。问题是我不能理解如何绑定我的tabItem模板(它有一个按钮)的命令。下面是代码:从样式模板中绑定按钮命令
(含有标签部分的,简化的用户控件..):
<UserControl.DataContext>
<vm:InicioViewModel />
</UserControl.DataContext>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Visual Resources/TabResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<ContentPresenter HorizontalAlignment="Stretch" Grid.Column="1">
<ContentPresenter.Content>
<TabControl Name="tc">
<TabControl.DataContext>
<vm:WorkSpaceViewModel/>
</TabControl.DataContext>
<TabControl ItemsSource="{Binding Items}"/>
</TabControl>
</ContentPresenter.Content>
</ContentPresenter>
</Grid>
(这里是为TabItem的资源字典):
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="TaBorder" Width="auto" Height="auto"
BorderBrush="LightGray"
BorderThickness="0.5,0.5,0.5,0"
CornerRadius="3,3,0,0"
Background="WhiteSmoke">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<TextBlock Margin="2" Grid.Column="0" Text="{TemplateBinding Header}" />
<Button x:Name="CloseButton" Grid.Column="1" Width="11" Height="11"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Transparent"
DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext}"
Command="{Binding CloseWorkSpaceCommand}"
(Donkt know what yo put in command to reference my viewmodel Icommand)
BorderThickness="0"
>
<!-- ETC -->
(Here my viewModel):
class InicioViewModel : ViewModelBase
{
private WorkSpaceViewModel _workSpaceVm;
public InicioViewModel()
{
}
public WorkSpaceViewModel WorkSpaceVm
{
get { return _workSpaceVm; }
set { _workSpaceVm = value; }
}
}
(WorkSpaceViewModel ..):
public class WorkSpaceViewModel
{
private ObservableCollection<IWorkSpaceItemVm> _items;
private RelayCommand _closeWorkSpaceCommand;
public WorkSpaceViewModel()
{
_items = new ObservableCollection<IWorkSpaceItemVm>();
}
public ObservableCollection<IWorkSpaceItemVm> Items
{
get { return _items; }
set { _items = value; }
}
public ICommand CloseWorkSpaceCommand
{
get
{
return _closeWorkSpaceCommand ?? (_closeWorkSpaceCommand = new RelayCommand(
param => CloseWorkSpace_Execute(param),
param => CloseWorkSpace_CanExecute(param)
));
}
}
private void CloseWorkSpace_Execute(object parm)
{
MessageBox.Show("asdasdasd");
}
private bool CloseWorkSpace_CanExecute(object parm)
{
return true;
}
}
正如你可以注意到我只有CloseWorkSpace_Execute显示用于测试目的一个消息。
1)我如何从我的tabItem样式模板中引用我的viewmodel中的Icommand,或者如果有更好的方法使用相同的结果将会受到欢迎。
2)为什么当我运行应用程序创建一个空的选项卡,我有我的观察集合列表为空
编辑: 的RelayCommand是工作在程序的其他部分确定,那不是问题, tabItem被渲染成可以触发工作和所有,我仍然不能让我的头脑如何从我的viewmodel绑定命令与我制作的模板tabItem。
编辑2: 该命令现在正在工作,显然该命令无法在资源字典中识别,并标记为:“在对象类型的DataContext中不能识别属性'CloseWorkSpaceCommand'”,但设置按钮的DataContext为: DataContext =“{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type TabControl}},Path = DataContext}” 运行应用程序时完成工作(Visual Studio仍然道歉DataContext类型,不知道它是什么手段)。 默认情况下还有一个选项卡,为什么? 是否有一种方法来纠正与DataContext类型的代码气味?
乍一看,似乎你的困难可能与安置模板e放在单独的资源字典中,使访问任何你想绑定的东西变得不那么方便。想到的一种可能的解决方案是在同一个XAML文件中声明一个合适的'Command'并绑定到该文件,然后在别处建立一个绑定(例如'CommandBinding')来响应该命令。但是,这里有太多的代码可以肯定地知道模糊问题的哪一部分给你带来麻烦,没关系花费时间来解决问题。 –
请编辑,以便只包含可靠地再现问题的[良好,_minimal_,_complete_代码示例](https://stackoverflow.com/help/mcve)。 –
嘿,彼得,我编辑它,我不明白你在说什么,你说问题是资源字典声明的参考?,那么,相对资源是什么?有另一种方法可以实现这一目标吗?我想绑定一个命令到样式模板中的按钮,无论我是否必须在模板中声明,内联在我的用户或我的tabcontrol资源中,但我不知道如何引用我在tabItem模板中创建的按钮 –