我想创建TabItem标头与使用户关闭标签的按钮。该对象的视觉表示和数据绑定就好了。TabControl与可关闭TabItem标头
我已经试验了DataContext,但到目前为止我还没有找到一个可行的解决方案。
我的XAML:
<TabControl
Grid.Column="3"
Grid.Row="2"
x:Name="TabControlTargets"
ItemsSource="{Binding Path=ViewModelTarget.IpcConfig.DatabasesList, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=ViewModelTarget.SelectedTab, UpdateSourceTrigger=PropertyChanged}">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock FontFamily="Calibri" FontSize="15" FontWeight="Bold" Foreground="{Binding FontColor}" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Margin="0,0,20,0"/>
<Button HorizontalAlignment="Left" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext}" Command="{Binding Path = ViewModelTarget.buttonRemoveDatabaseCommand}"
CommandParameter="**?**"
>
<Button.Content>
<Image Height="15" Width="15" Source="pack://application:,,,/Images/cancel.png" />
</Button.Content>
</Button>
</StackPanel>
</DataTemplate>
我有麻烦搞清楚如何设置我的按钮的CommandParameter,以便它指的是正确的对象。
这里是我的RelayCommand:
public ICommand buttonRemoveDatabaseCommand
{
get
{
if (_buttonRemoveDatabaseCommand == null)
{
_buttonRemoveDatabaseCommand = new RelayCommand(
param => RemoveDatabase(param)
);
}
return _buttonRemoveDatabaseCommand;
}
}
这里是我的RemoveDatabase功能:
public void RemoveDatabase(object dB)
{
this.IpcConfig.RemoveDataBase((PCDatabase)dB);
}
我强烈希望的是坚持我的做法 “背后没有代码” 的解决方案。
你能展示你的viewmodel – Flaugzig
那么'SelectedItem'呢?它有什么问题? –
@Eugene Podksal使用SelectedItem是我的第一个方法。但是,这将始终关闭所选的选项卡(即活动选项卡),该选项卡不一定是应该关闭的选项卡。 – Fang