Why doesn't my ToggleButton style work on the second tab of the TabControl?

I had a problem when I was developing a WPF application with a TabControl object. I tried to debug and find the problem, and finally I succeeded, but I did not find a workaround for it. Here are a few explanations:

I used this data mesh filtering library ( here is the codeproject code ), which is the best (from my point of view). I want to customize it using the Google Material Design Theme and change some graphic functions, for example, using the toggle button in the header of the first tab as a data row to hide / show the filtering option.

I created a user control and placed my own datagrid in it. Then I embedded this control in tabItem. When I set this control to the first tabItem, everything works correctly. But when I change the user control to another tabItem, the toggle button does not work.

Here is my main xaml window code that didn't work:

 <TabControl x:Name="tabControl">
        <TabItem Header="1'st Tab">
            <ContentControl DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
                <Button Content="Do no thing"></Button>
            </ContentControl>
        </TabItem>
        <TabItem Header="2'nd Tab">
            <ContentControl DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
                <local:UserControl1/>
            </ContentControl>
        </TabItem>
 </TabControl>

Please note that if I reorder TabItems, it works well. Anyone have a suggestion to solve this problem? Here is my sample project code on Github

The control is located on the first tab The control is placed on a different tab.

: "WPF Inspector", . , , "WPF Inspector" , . GIF - , :

Strange GIF behavior

+4
1

ContentControl , , Content DataTemplate. DataTemplate DataContext .

:

    <TabControl x:Name="tabControl">
        <TabItem Header="1'st Tab">
            <ContentControl Content="{Binding .}">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <Button Content="Do no thing"></Button>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>
        </TabItem>
        <TabItem Header="2'nd Tab">
            <ContentControl Content="{Binding .}">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <local:UserControl1/>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>
        </TabItem>
    </TabControl>
+1

Source: https://habr.com/ru/post/1658598/


All Articles