我想創建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