Binding two user controls using MultiDataTrigger

2019-08-17 07:17发布

问题:

I have the following code snippet to enable a binding for the condition of MultiDataTrigger.

  <StackPanel Grid.Column="2">
        <Expander IsExpanded="True" x:Name="test1">
            <TextBlock Text="In Expander test1" />
        </Expander>
        <StackPanel Orientation="Vertical"  VerticalAlignment="Top">
            <views:BarPartDefView x:Name="Exp11" DataContext="{Binding CurrentPartVM}" />
        </StackPanel>
        <GridSplitter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="4" >
            <GridSplitter.Style>
                <Style TargetType="GridSplitter">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=test1,  Path=IsExpanded}" Value="True" />
                                <Condition Binding="{Binding ElementName=Exp11,  Path=IsExpanded}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Visibility" Value="Visible" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </GridSplitter.Style>
        </GridSplitter>
    </StackPanel>

I have the following code snippet in the other XAML file.

 <UserControl.Style>
        <Style TargetType="local:BarPartDefView">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=Exp1, Path=IsExpanded}" Value="False"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsExpanded" Value="False"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>

    <StackPanel>
        <Expander IsExpanded="False" x:Name="Exp2">
            <TextBlock Text="In Expander part 1 " />
        </Expander>
        <Expander IsExpanded="False" x:Name="Exp1">
            <TextBlock Text="In Expander part 2" />
        </Expander>
    </StackPanel>

I have created a dependency property for the class BarPartDefView.

public static DependencyProperty IsExpandedProperty = DependencyProperty.Register(
        "IsExpanded",
        typeof(Boolean),
        typeof(BarPartDefView),
        new FrameworkPropertyMetadata {
        BindsTwoWayByDefault = false,
        DefaultValue = true,
        DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
        PropertyChangedCallback = (sender, e) => {
            var i = 0;
        }});
public bool IsExpanded
{
    get { return (bool)GetValue(IsExpandedProperty); }
    set { SetValue(IsExpandedProperty, value); }
}

I am not able to bind the second condition of MultiDataTrigger which is

<Condition Binding="{Binding ElementName=Exp11,  Path=IsExpanded}" Value="True" />

I saw a solution online but it binds only the elementname in the same user control. I want to bind to a elementname in another User control. How can I accomplish it. Is it possible to have such a binding?. Since I am relative new to wpf so I am exploring all the possible ways of doing it. it would be really helpful if you can help me out.