fluentribbon / Fluent.Ribbon

WPF Ribbon control like in Office
http://fluentribbon.github.io
MIT License
2.55k stars 517 forks source link

The elements in the ribbon use ElementName = xxx to bind the data and cannot find the xxx element #798

Closed 845669495 closed 4 years ago

845669495 commented 4 years ago

Conditions that trigger bugs:

  1. Ribbon exists in TabItem under TabControl;
  2. TabControl has multiple TabItems;
  3. The TabItem where the Ribbon is located is not selected when starting the software;

When the above conditions are met, any element in the Ribbon cannot find the element by data binding using the "{Binding ElementName = xxx}" method.

Hope this bug can be fixed, thank you very much!


Environment

845669495 commented 4 years ago
<TabControl>
    <TabItem Header="tab1" IsSelected="True">
    </TabItem>
    <TabItem Header="tab2">
        <StackPanel>
            <Fluent:Ribbon>
                <Fluent:RibbonTabItem Header="Main">
                    <Fluent:RibbonGroupBox>
                        <Fluent:ToggleButton LargeIcon="1.png" IsChecked="{Binding ElementName=cb1,Path=IsChecked}"></Fluent:ToggleButton>
                    </Fluent:RibbonGroupBox>
                </Fluent:RibbonTabItem>
            </Fluent:Ribbon>
            <CheckBox x:Name="cb1">checkBox1</CheckBox>
        </StackPanel>
    </TabItem>
</TabControl>

The above is the example code. Normally, the selected state of ToggleButton should follow checkBox1, but in fact this data binding has not worked.

batzen commented 4 years ago

That's nothing i can do about. ElementName only works if the element has been loaded. What you could try is to use TabControlEx from ControlzEx and select every tab in your tabcontrol once when your UI is loaded and set the selection back to the initial tab afterwards. TabControlEx keeps the visual tree of every tab in memory once it has been selected. The regular TabControl only keeps the currently selected tabs content in memory and unloads the content from unselected tabs. Hope this helps.

845669495 commented 4 years ago

But WPF native controls do not have this problem. Is there any difference between Ribbon and native controls?

batzen commented 4 years ago

Do you have a sample where regular wpf controls don't have this issue?

845669495 commented 4 years ago
<TabControl>
    <TabItem Header="tab1" IsSelected="True">
    </TabItem>
    <TabItem Header="tab2">
        <StackPanel>
            <CheckBox x:Name="cb1">checkBox1</CheckBox>
            <CheckBox x:Name="cb2" IsChecked="{Binding ElementName=cb1,Path=IsChecked}">checkBox2</CheckBox>
            <Fluent:Ribbon>
                <Fluent:RibbonTabItem Header="Main">
                    <Fluent:RibbonGroupBox>
                        <CheckBox x:Name="cb3" IsChecked="{Binding ElementName=cb1,Path=IsChecked}">checkBox3</CheckBox>
                    </Fluent:RibbonGroupBox>
                </Fluent:RibbonTabItem>
            </Fluent:Ribbon>
        </StackPanel>
    </TabItem>
</TabControl>

The above is the example code. The data binding of checkBox1 and checkBox2 can work, but the data binding of checkBox1 and checkBox3 does not work.

The following is the output log System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=cb1'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'CheckBox' (Name='cb3'); target property is 'IsChecked' (type 'Nullable`1')

845669495 commented 4 years ago

666

batzen commented 4 years ago

Will have a look at this when i find some spare time.

batzen commented 4 years ago

This is fixed in version 8.0.