如果现在可以看到一个或多个其他StackPanel,则尝试使用Binding设置StackPanel可见性

时间:2016-06-20 17:56:27

标签: c# wpf binding visibility

我有GridStackPanel个对象。

右侧的面板有4个垂直ToggleButton个对象。

4个 StackPanel 对象使用绑定作为可见性。例如:

<StackPanel x:Name="panelMaterialAndSettings" Grid.Row="3" Background="WhiteSmoke" Margin="2" 
            Visibility="{Binding IsChecked, ElementName=buttonMaterialAndSettings, Converter={StaticResource BoolToVis}, FallbackValue=Collapsed}">

效果很好。我无法理解的问题是父StackPanel。这也需要绑定Vibility属性,但如果现在可以看到4个子面板中的一个或多个,则它只返回true。

如果无法解决问题,我可以使用手动代码完成所有操作。

或者,它可以检查4个ToggleButtons。但我失败了。

标记:

<Grid Grid.Row="1" Grid.RowSpan="4" Grid.Column="2" Margin="2,0,2,2">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel x:Name="panelExtraFeatures" Visibility="Collapsed" MinWidth="500" Grid.Column="0">
        <StackPanel.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="{DynamicResource {x:Static SystemColors.AppWorkspaceColorKey}}" Offset="1"/>
            </LinearGradientBrush>
        </StackPanel.Background>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <StackPanel x:Name="panelStudentPicker" Grid.Row="0" Background="WhiteSmoke" Margin="2,2,2,10" <StackPanel x:Name="panelStudentPicker" Grid.Row="0" Background="WhiteSmoke" Margin="2,2,2,10"
                            Visibility="{Binding IsChecked, ElementName=buttonStudentPicker, Converter={StaticResource BoolToVis}, FallbackValue=Collapsed}">
                <Label Content="Student Picker" Background="LightSkyBlue" />
                <Label>Panel 1</Label>
                <Label>Panel 1</Label>
                <Label>Panel 1</Label>
                <Label>Panel 1</Label>
            </StackPanel>
            <StackPanel x:Name="panelDemonstrationPicker" Grid.Row="1" Background="WhiteSmoke" Margin="2,2,2,10"
                        Visibility="{Binding IsChecked, ElementName=buttonDemonstrationPicker, Converter={StaticResource BoolToVis}, FallbackValue=Collapsed}">
                <Label  Content="DemonStration Picker" Background="LightSkyBlue" />
                <Label>Panel 2</Label>
                <Label>Panel 2</Label>
                <Label>Panel 2</Label>
                <Label>Panel 2</Label>
            </StackPanel>
            <StackPanel x:Name="panelAssignmentHistory" Grid.Row="2" Background="WhiteSmoke" Margin="2,2,2,10"
                        Visibility="{Binding IsChecked, ElementName=buttonAssignmentHistory, Converter={StaticResource BoolToVis}, FallbackValue=Collapsed}">
                <Label  Content="Assignment History" Background="LightSkyBlue" />
                <Label>Panel 3</Label>
                <Label>Panel 3</Label>
                <Label>Panel 3</Label>
                <Label>Panel 3</Label>
            </StackPanel>
            <StackPanel x:Name="panelMaterialAndSettings" Grid.Row="3" Background="WhiteSmoke" Margin="2" 
                        Visibility="{Binding IsChecked, ElementName=buttonMaterialAndSettings, Converter={StaticResource BoolToVis}, FallbackValue=Collapsed}">
                <Label  Content="Material and Settings" Background="LightSkyBlue" />
                <GroupBox Header="Treasures From God's Word:" Margin="2">
                    <StackPanel HorizontalAlignment="Stretch">
                        <Label>Bible Reading:</Label>
                        <TextBox/>
                    </StackPanel>
                </GroupBox>
                <GroupBox Header="Apply Yourselves To The Field Ministry" Margin="2">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition Width="2*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">Student Assignment #1:</Label>
                        <ComboBox Grid.Row="1" Grid.Column="0" IsEditable="True" SelectedIndex="0" Margin="2">
                            <ComboBoxItem Content="Initial Call"/>
                            <ComboBoxItem Content="Return Visit"/>
                            <ComboBoxItem Content="Bible Study"/>
                        </ComboBox>
                        <TextBox Grid.Row="1" Grid.Column="1"/>
                        <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">Student Assignment #2:</Label>
                        <ComboBox Grid.Row="3" Grid.Column="0" IsEditable="True" SelectedIndex="1" Margin="2">
                            <ComboBoxItem Content="Initial Call"/>
                            <ComboBoxItem Content="Return Visit"/>
                            <ComboBoxItem Content="Bible Study"/>
                        </ComboBox>
                        <TextBox Grid.Row="3" Grid.Column="1"/>
                        <Label Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2">Student Assignment #3:</Label>
                        <ComboBox Grid.Row="5" Grid.Column="0" IsEditable="True" SelectedIndex="2" Margin="2">
                            <ComboBoxItem Content="Initial Call"/>
                            <ComboBoxItem Content="Return Visit"/>
                            <ComboBoxItem Content="Bible Study"/>
                        </ComboBox>
                        <TextBox Grid.Row="5" Grid.Column="1"/>
                    </Grid>
                </GroupBox>
            </StackPanel>
        </Grid>
    </StackPanel>
    <StackPanel Grid.Column="1" Margin="2,0,2,2">
        <StackPanel.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="{DynamicResource {x:Static SystemColors.ActiveCaptionColorKey}}"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </StackPanel.Background>

        <ToggleButton x:Name="buttonStudentPicker" Content="Student Picker" HorizontalAlignment="Right" Margin="2,10,2,10" BorderThickness="0,5,0,0" Background="{x:Null}">
            <ToggleButton.LayoutTransform>
                <RotateTransform Angle="90"/>
            </ToggleButton.LayoutTransform>
        </ToggleButton>
        <ToggleButton x:Name="buttonDemonstrationPicker" Content="Demonstrations Picker" HorizontalAlignment="Right" Margin="2,2,2,10" BorderThickness="0,5,0,0" Background="{x:Null}">
            <ToggleButton.LayoutTransform>
                <RotateTransform Angle="90"/>
            </ToggleButton.LayoutTransform>
        </ToggleButton>
        <ToggleButton x:Name="buttonAssignmentHistory" Content="Assignment History" HorizontalAlignment="Right" Margin="2,2,2,10" BorderThickness="0,5,0,0" Background="{x:Null}">
            <ToggleButton.LayoutTransform>
                <RotateTransform Angle="90"/>
            </ToggleButton.LayoutTransform>
        </ToggleButton>
        <ToggleButton x:Name="buttonMaterialAndSettings" Content="Material and Settings" HorizontalAlignment="Right" Margin="2" BorderThickness="0,5,0,0" Background="{x:Null}">
            <ToggleButton.LayoutTransform>
                <RotateTransform Angle="90"/>
            </ToggleButton.LayoutTransform>
        </ToggleButton>
    </StackPanel>

</Grid>

1 个答案:

答案 0 :(得分:0)

其他人提请我注意MultiDataTrigger

因此,我能够在这个主题上找到一个好的resource

我能够提出以下建议:

<StackPanel.Style>
    <Style TargetType="StackPanel">
        <Setter Property="Visibility" Value="Visible" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=buttonStudentPicker, Path=IsChecked}" Value="False" />
                    <Condition Binding="{Binding ElementName=buttonDemonstrationPicker, Path=IsChecked}" Value="False" />
                    <Condition Binding="{Binding ElementName=buttonAssignmentHistory, Path=IsChecked}" Value="False" />
                    <Condition Binding="{Binding ElementName=buttonMaterialAndSettings, Path=IsChecked}" Value="False" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Visibility" Value="Collapsed" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</StackPanel.Style>