How to prevent loss of style of a button when the main container loses focus?

1

I have a UserControl that contains several elements, but there are two that are giving me problems; a ToggleButton and a TreeView .

When I have information in the TreeView, when I select an element (it is worth mentioning that I have three HierarchicalDataTemplate in the TreeView) and then I make a .Clear() to the model, the style of ToggleButton disappears.

In the style of the button, place MultiTrigger to check if it was selected and if the UserControl did not have the Focus . If this is true, I keep the correct style.

<MultiTrigger.Conditions>
    <Condition Property="UserControl.IsFocused" Value="False" />
    <Condition Property="ToggleButton.IsChecked" Value="True" />
</MultiTrigger.Conditions>

This worked for me in some situations, but there are others in which I do not.

"Works" when the TreeView has elements in the SortDescription , but if it does not have them, the style of the button disappears and it looks like it is inactive.

Place functions in events LostFocus & GotFocus of UserControl to detect the losses and gain of focus in the window, but when deleting the model of the TreeView the LostFocus is not executed.

Edition

WPF Current of my TreeView

<TreeView Name="NetworkTreeView" Grid.Row="1" Grid.Column="0" SelectedItemChanged="NetworkTreeView_SelectedItemChanged" TreeViewItem.Expanded="TreeView_ExpandedChange" TreeViewItem.Collapsed="TreeView_ExpandedChange">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type self:NetTreeProtocol}" ItemsSource="{Binding Channels}">
            <StackPanel Orientation="Horizontal" Style="{StaticResource TreeViewMenu}">
                <Image Source="/Resources/Images/NodesGroup.png" Margin="0,0,5,0" />
                <TextBlock Text="{Binding Label}" FontWeight="Bold"/>
                <TextBlock Text="{Binding LabelCount}" Style="{StaticResource LabelCountStyle}"/>                
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type self:NetTreeChannel}" ItemsSource="{Binding Pans}">
            <StackPanel Orientation="Horizontal" Style="{StaticResource TreeViewMenu}">
                <Image Source="/Resources/Images/NodesGroup.png" Margin="0,0,5,0" />
                <TextBlock Text="{Binding Label}"  FontWeight="Bold"/>
                <TextBlock Text="{Binding LabelCount}" Style="{StaticResource LabelCountStyle}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type self:NetTreePan}">
            <HierarchicalDataTemplate.ItemsSource>
                <MultiBinding Converter="{StaticResource SortNodes}">
                    <Binding Path="Nodes"/>
                    <Binding Path="IsChecked" ElementName="RdSortByArrival"/>
                    <Binding Path="IsChecked" ElementName="RdSortByShort"/>
                    <Binding Path="IsChecked" ElementName="RdSortByLong"/>
                    <Binding Path="IsChecked" ElementName="BtnSortOptions"/>
                </MultiBinding>
            </HierarchicalDataTemplate.ItemsSource>
            <StackPanel Orientation="Horizontal" Style="{StaticResource TreeViewMenu}">
                <Image Source="/Resources/Images/NodesGroup.png" Margin="0,0,5,0" />
                <TextBlock Text="{Binding Label}"  FontWeight="Bold"/>
                <TextBlock Text=" - " Style="{StaticResource LongAddressStyle}"/>
                <TextBlock Text="{Binding LabelLongAddress}" FontWeight="Bold" Name="itemLongAddress" Style="{StaticResource LongAddressStyle}"/>
                <TextBlock Text="{Binding LabelCount}" Style="{StaticResource LabelCountStyle}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type self:NetTreeNode}">
            <Grid Width="Auto">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="17"/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Source="/Resources/Images/NodesItem.png" Margin="0,0,5,0" />
                <TextBlock Grid.Column="1" Text="{Binding Label}" />
                <TextBlock Grid.Column="2" Text="{Binding LabelLongAddress}" Name="itemLongAddress" Style="{DynamicResource LongAddressStyle}"/>
            </Grid>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

CS current of UserControl

public Constructor()
{
    InitializeComponent();
    viewModel = new NETreeViewModel();
    NetworkTreeView.DataContext = viewModel;
    NetworkTreeView.ItemsSource = viewModel.Protocols;
    _updateDataIsNeeded = true;
}

And my method Clear , this is called from the main window along with other methods Clear other UserControl .

public void Clear()
{
    lock (SyncRoot)
    {
        Dispatcher.Invoke(new Action(() => viewModel.Protocols.Clear()));
        _lastNodeSelected = null;
    }
}

I hope I explained my problem. Thanks in advance.

    
asked by Tecnologer 22.04.2017 в 01:57
source

0 answers