Controlling the TabControl active tab with a ComboBox

前端 未结 4 795
北海茫月
北海茫月 2021-01-20 00:32

What i am really trying to achieve is to full control the active TabItem by using the combobox as the navigation control.

Here is what ive got so far:



        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 00:57

    I've been playing with this a lot. There is something about the source of the ComboBox's databinding changing after the selection is made. Or something.

    I added the Diagnostics namespace:

    xmlns:debug="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    

    And I changed your into TextBoxes with big honking numbers so that I could see that things really were changing:

        
            
        
    

    And when I ran the app, I found that Diagnostics reports a weird result:

    System.Windows.Data Error: 39 : BindingExpression path error: 'Header' property not found on 'object' ''TextBlock' (Name='tb1')'. BindingExpression:Path=Header; DataItem='TextBlock' (Name='tb2'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

    I tried to set the databinding once, in code, at startup:

        public Window1()
        {
            InitializeComponent();
    
            Binding positionBinding = new Binding("Items");
            positionBinding.ElementName = "TabControl1";
            positionBinding.Path = new PropertyPath("Items");
            positionBinding.Mode = BindingMode.OneTime;
    
            CmbTabs.SetBinding(ComboBox.ItemsSourceProperty, positionBinding);
            CmbTabs.DisplayMemberPath = "Header";
        }
    

    And it still switches the CombobBox to show no selected item after the selection and change of TabItem is made. It's as if the DataContext is switched to the TabControl.TabItem.TextBlock after the TabControl changes selection.

    So I don't exactly have an answer for you but I have some results for you to work on.

    Bea Stollnitz has a good article on using this diagnostic technique. "How can I debug WPF bindings?"

提交回复
热议问题