jeudi 21 juillet 2016

WPF C# accessing a checkbox in a custom Tabitem control

I have a tab control that I create tab items dynamically. On the tab items, I populate text boxes and checkboxes based on the hardware of the machine. The tab item is a custom control with dependency properties. When I create the tab items, I don't know how to access the events.

The first one is my "working code" but I can't react to the state of a checkbox from the main form. i.e. disable a save button if the box is unchecked.

From MainWindow.xaml:

<TabControl x:Name="tabSettings"
    Grid.Row="2"
    Grid.Column="0"
    Grid.ColumnSpan="5"
    Margin="10,10,10,10"
    Background="#FF1B1B1B"
    ItemsSource="{Binding}"
    SelectionChanged="tabSettings_SelectionChanged" />

This next example gets me access to the events, but it doesn't populate the tab control with tab items, which I do understand why it doesn’t.

<tabcntrl:TabDataControl x:Name="tabSettings"
    Grid.Row="2"
    Grid.Column="0"
    Grid.ColumnSpan="5"
    Margin="10,10,10,10"
    UseDHCP_Click="NICTabs_UseDHCP_Click"
    UseDNS_Click="NICTabs_UseDNS_Click"
    Background="#FF1B1B1B" />

I don’t understand how to access the tab item events in the tab control from the main window.

From MainWindow.xaml.cs:

    private void createTabs()
    {
        foreach ( var nicAdapter in NICAdapters )
        {
            string newTabeName = nicAdapter.AdapterName;

            //Create a new tab and set the data context.
            var NIC_Tab = new TabItem()
            {
                Header = newTabeName,
                Content = new TabDataControl(),
                DataContext = nicAdapter
            };

            tabSettings.Items.Add( NIC_Tab );

        }

        if ( tabSettings != null )
        {
            Style style = new Style( typeof( TabItem ), Application.Current.FindResource( "tabItemStyle" ) as Style );
            tabSettings.Resources.Add( typeof( TabItem ), style );
        }
        tabSettings.SelectedIndex = 0;
    }

Any guidance where I went wrong and what I am missing? I will be very grateful.

Thanks

Jeff




Aucun commentaire:

Enregistrer un commentaire