Search code examples
c#wpfxpathbindingmultidatatrigger

Convert This Binding to MultiDataTrigger Binding


I have this binding that we use to selectively display/hide elements:

<Binding XPath="InputFileIsNeeded">
    <Binding.Converter>
        <tl:IsEnabledToStringConverter 
            DisabledValue="Collapsed"
            EnabledValue="Visible" />
    </Binding.Converter>
</Binding>

I now have an element that I need to selectively show/hide based on the above binding AND a binding to the command line argument 'Setup':

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.StartArg}" Value="Setup">
    <Setter Property="Visibility" Value="Visible" />
</DataTrigger>

I need to apply both of these bindings to the following so the following element is only shown when in 'Setup' mode and when 'InputFileIsNeeded' is true:

    <Style x:Key="ColumnCountSpinner" TargetType="{x:Type ScrollBar}">
        <Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="MinHeight" Value="25"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>                

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Label Content="Column Count:" 
                                   Grid.Column="0" />

                        <Border BorderThickness="1" BorderBrush="Gray" Grid.Column="1">
                            <Grid Margin="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock VerticalAlignment="Center" FontSize="12" MinWidth="25" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0" />
                                <Grid Grid.Column="1" x:Name="GridRoot" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Background="{TemplateBinding Background}">
                                    <Grid.RowDefinitions>
                                        <RowDefinition MaxHeight="18"/>
                                        <RowDefinition Height="0.00001*"/>
                                        <RowDefinition MaxHeight="18"/>
                                    </Grid.RowDefinitions>
                                    <RepeatButton x:Name="DecreaseRepeat" Command="ScrollBar.LineDownCommand" Focusable="False">
                                        <Grid>
                                            <Path x:Name="DecreaseArrow" Stroke="{TemplateBinding Foreground}" StrokeThickness="1" Data="M 0 4 L 8 4 L 4 0 Z"/>
                                        </Grid>
                                    </RepeatButton>
                                    <RepeatButton Grid.Row="2" x:Name="IncreaseRepeat" Command="ScrollBar.LineUpCommand" Focusable="False">
                                        <Grid>
                                            <Path x:Name="IncreaseArrow" Stroke="{TemplateBinding Foreground}" StrokeThickness="1" Data="M 0 0 L 4 4 L 8 0 Z"/>
                                        </Grid>
                                    </RepeatButton>
                                </Grid>
                            </Grid>
                        </Border>
                    </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>

I am having difficulty with how to bind the XPath to a MultiDataTrigger. I have tried to following but get two errors (1) Expected '}' (on "XPath=") and (2) I obviously cannot use here as there is no specific element (I think this would be inside the Condition element).

Can someone show me how to bind these two properties using MultiDataTrigger or some other mechanism?


Solution

  • (The following information was provided by the question author in an edit.)

    I found an example that I was able to modify to my specific case. The following is added to the Style in the OP:

    <Style.Triggers>
        <MultiDataTrigger>
            <!-- Condition for 'Setup' and InputFileIsNeeded = true -->
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.StartArg}" Value="Setup" />
                <Condition Binding="{Binding XPath=InputFileIsNeeded}" Value="true" />
            </MultiDataTrigger.Conditions>
        </MultiDataTrigger>
    <Style.Triggers>