将MenuItem的IsChecked绑定到TabItem的isSelected

时间:2014-11-14 15:57:51

标签: c# wpf xaml menuitem tabitem

基本上我遇到的问题与Binding MenuItem's IsChecked to TabItem's IsSelected with dynamic tabs中描述的问题相同 我定制TabControl有自己的viewModel,我也有一个绑定到同一个源的菜单。 发生了什么事情是绑定menuItem' isCheckedisSelected不再有效。 I thought IsSelected can not be found as there's no such property in viewModel

<Setter Property="IsChecked" Value="{Binding IsSelected, Mode=TwoWay}" />

我尝试使用建议的解决方案来构建TabItem列表,但我收到错误Unable to cast object of type TabData to type TabItem。下面是我的xaml和转换器。我认为它失败了,因为在构造期间TabControl.items将返回viewmodel实例而不是UIControl TabItem;有什么建议如何在这里做绑定?

XAML

<Menu Background="Transparent">
    <MenuItem
         Style="{StaticResource TabMenuButtonStyle}"
         ItemsSource="{Binding RelativeSource=
            {RelativeSource FindAncestor, 
            AncestorType={x:Type TabControl}}, 
            Path=Items,Mode=OneWay,NotifyOnSourceUpdated=True,Converter={StaticResource TabControlItemConverter}}"
         ItemContainerStyle="{StaticResource TabMenuItemxxx}">
    </MenuItem>
</Menu>

C#

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    ItemCollection ic = (ItemCollection)value;
    List<TabItem> tabItems = new List<TabItem>();
    foreach (var obj in ic)
    {
        tabItems.Add((TabItem)obj);
    }
    return tabItems;
}

enter image description here

1 个答案:

答案 0 :(得分:2)

以下是基于所提供项目的更改

从绑定中删除以下内容,不需要

,Mode=OneWay,NotifyOnSourceUpdated=True,Converter={StaticResource TabControlItemConverter}

修改样式TabMenuItemxxx

中的setter

来自

<Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=TabItem}}" />

<Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay/>

TargetType="{x:Type TabItem}"

的样式中添加以下setter
<Setter Property="IsSelected" Value="{Binding IsSelected}" />

修改TabData类,如下所示

public class TabData : INotifyPropertyChanged
{
    private bool isselected;
    public string Header { get; set; }
    public object Content { get; set; }
    public bool IsEnabled { get; set; }
    public bool IsSelected
    {
        get { return isselected; }
        set
        {
            if (ViewModel.CurrentItem.IsSelected && ViewModel.CurrentItem != this)
            {
                ViewModel.CurrentItem.IsSelected = false;
            }
            isselected = value;
            RaisePropertyChanged("IsSelected");

            if (ViewModel.CurrentItem != this)
                ViewModel.CurrentItem = this;

        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这就是您需要更改项目以同步菜单项并选中选项卡项目。

关于关闭标签项的第二个问题,您可以通过更改关闭按钮修复它

CommandParameter="{Binding SelectedItem,ElementName=tabControl}"

CommandParameter="{Binding}"

示例项目TabControlSyncWithMenuItems.zip

让我知道结果。