网格 - 隐藏行和文本块显示问题

时间:2016-09-05 21:03:17

标签: c# wpf xaml

我有一个简单的对话框 - Grid有4个TextBlock,其中我将第2个和第3个TextBlock的可见性绑定到属性但是它没有按预期工作即,在查看对话框时,条件消息显示为截断。对话框的两个变体如下:

一些文字

条件消息1

B C

一些文字

条件消息2

B C

对话框的XAML如下所示。

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:cl="http://www.caliburnproject.org"
            xmlns:local="clr-namespace:ABC.DrillDown"
            xmlns:iwpf="clr-namespace:ABC.Mvvm.Wpf;assembly=ABC.Mvvm.Wpf"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="ABC.DrillDown.Views.MessageView"
            Title="{Binding WindowTitle}"  WindowStartupLocation="CenterScreen" MaxWidth="525" MinWidth="525" Background="White" MinHeight="275" MaxHeight="275">
    <Window.Resources>
            <iwpf:BoolToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
        </Window.Resources>
        <Grid Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="514" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" MinHeight="64"/>
                <RowDefinition Height="{Binding TransactionsModified, Converter={StaticResource booleanToVisibilityConverter}}">
                    <RowDefinition.Style>
                        <Style TargetType="{x:Type RowDefinition}">
                            <Setter Property="Height" Value="Auto" />
                            <Style.Triggers>
<!--Hide Row-1-->
                                <DataTrigger Binding="{Binding TransactionsModified}" Value="False">
                                    <Setter Property="Height" Value="0" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RowDefinition.Style>
                </RowDefinition>
                <RowDefinition Height="{Binding TransactionsDeleted, Converter={StaticResource booleanToVisibilityConverter}}">
                    <RowDefinition.Style>
                        <Style TargetType="{x:Type RowDefinition}">
                            <Setter Property="Height" Value="Auto" />
                            <Style.Triggers>
<!--Hide Row-2-->
                                <DataTrigger Binding="{Binding TransactionsDeleted}" Value="false">
                                    <Setter Property="Height" Value="0" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </RowDefinition.Style>
                </RowDefinition>
                <RowDefinition Height="Auto" MinHeight="80"/>
                <RowDefinition Height="Auto" MinHeight="30"/>
                <RowDefinition Height="Auto" MinHeight="12"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock  Grid.Row="0" Grid.Column="0" TextWrapping="NoWrap" HorizontalAlignment="Left" xml:space="preserve" VerticalAlignment="Top" Height="64" Width="478">Some Text           </TextBlock>
                <TextBlock  Grid.Row="1" Grid.Column="0" Text="{Binding TransactionsModified}" Visibility="{Binding TransactionsModified, Converter={StaticResource booleanToVisibilityConverter}}" TextWrapping="NoWrap" HorizontalAlignment="Left"  xml:space="preserve" Width="0" >
               Conditional Text - 1
            </TextBlock>

            <TextBlock  Grid.Row="2" Grid.Column="0" Text="{Binding TransactionsDeleted}" Visibility="{Binding TransactionsDeleted, Converter={StaticResource booleanToVisibilityConverter}}" TextWrapping="NoWrap" HorizontalAlignment="Left" xml:space="preserve" Width="0">
                Conditional Text - 2
            </TextBlock>
            <TextBlock  Grid.Row="3" Grid.Column="0" TextWrapping="NoWrap" HorizontalAlignment="Left" xml:space="preserve" Width="425">
                Text 3

                Text 4
            </TextBlock>
            </Grid>
            </Window>

C

1 个答案:

答案 0 :(得分:1)

你有一些RowDefinition,其中有一个错误的高度(你应该删除这部分)

   Height="{Binding TransactionsModified, Converter={StaticResource booleanToVisibilityConverter}}">

当然将Height直接绑定到bool是没有意义的:你已经在下面的部分中有了更合理的触发器(它们看起来很好看)