如何安排元素?

时间:2010-10-21 07:20:12

标签: wpf xaml user-controls

我需要将对象放置40%40%20%并且没有滚动

 <Grid Grid.Row="1">
 <Grid.RowDefinitions>
  <RowDefinition Height="40*"  />
  <RowDefinition Height="40*"  />
  <RowDefinition Height="20*"/>
 </Grid.RowDefinitions>
 <Grid.ColumnDefinitions>
  <ColumnDefinition/>
  <ColumnDefinition/>
  <ColumnDefinition/>
  <ColumnDefinition/>
 </Grid.ColumnDefinitions>
 <DockPanel x:Name="stack_d"  Grid.Row="0" Grid.ColumnSpan="4"  VerticalAlignment="Stretch"  >
  <l:CategoryView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
 </DockPanel>
 <DockPanel x:Name="stack_trainer" Grid.Row="1" Grid.ColumnSpan="4"   DockPanel.Dock="Bottom" >
  <loc:TrainerView />
 </DockPanel>

 <Button Content="Почта" x:Name="mails" Grid.Row="2" Grid.Column="0"  Width="100" HorizontalAlignment="Center" VerticalAlignment="Stretch"  Click="mails_Click"/>
 <Button Content="Оплатить" Width="100" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"  Click="Button_Click_Payment"/>
 <Button Content="Тех помощь" Width="100" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Stretch"  x:Name="support" Click="support_Click"/>
 <Button Content="Настройки" x:Name="settings" Grid.Row="2" Grid.Column="3"   Width="100" HorizontalAlignment="Center" VerticalAlignment="Stretch" Click="settings_Click"/>
</Grid>

但我有问题。看图像

alt text

alt text

UserControl顶部:

<ScrollViewer VerticalScrollBarVisibility="Auto" >
    <Border>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock Text="Категории" Grid.Row="0" HorizontalAlignment="Center" Style="{StaticResource TextBlockTitle}"/>
            <ListBox Grid.Row="1" x:Name="list_category"  ItemsSource="{Binding Tables[0]}">
                <ListBox.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00FFFFFF"/>
                </ListBox.Resources>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <GroupBox Header="Категория" Width="300" HorizontalAlignment="Stretch" x:Name="GroupBox">
                            <DockPanel Tag="{Binding id}">
                                <Button Click="Button_Click" DockPanel.Dock="Top" >
                                    <Button.Content>
                                        <DockPanel>
                                            <TextBlock Text="{Binding title}" TextWrapping="Wrap" DockPanel.Dock="Top" Padding="5"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
                                            <l:ScrollViewerEx  VerticalScrollBarVisibility="Auto" >
                                                <TextBlock Text="{Binding description}" DockPanel.Dock="Top" TextWrapping="Wrap" Padding="5" IsHitTestVisible="False" />
                                            </l:ScrollViewerEx>
                                        </DockPanel>
                                    </Button.Content>
                                </Button>
                            </DockPanel>
                        </GroupBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Border>
</ScrollViewer>

下一个UserControl

<ScrollViewer VerticalScrollBarVisibility="Auto" Background="Transparent">
 <Border Background="Transparent">
  <Grid>
   <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition />
   </Grid.RowDefinitions>
   <TextBlock Text="Сопровождающие" Grid.Row="1" HorizontalAlignment="Center" Style="{StaticResource TextBlockTitle}"/>
   <ListBox Grid.Row="2" x:Name="list_category" Background="Transparent"  ItemsSource="{Binding Tables[0]}"  VerticalContentAlignment="Stretch"  >
    <ListBox.Resources>
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00FFFFFF"/>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
     <DataTemplate>
      <GroupBox Header="Сопровождающие" Width="300"  VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"  HorizontalAlignment="Stretch" x:Name="GroupBox" Background="Transparent">
       <DockPanel Tag="{Binding id_Person}"  VerticalAlignment="Stretch" Background="Transparent">
        <Button Click="Button_Click" DockPanel.Dock="Top">
         <Button.Content>
          <DockPanel VerticalAlignment="Stretch">
           <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Center">
            <TextBlock Text="{Binding surname}" Padding="5"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="{Binding name}" Padding="5"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="{Binding lastname}" Padding="5"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
           </StackPanel>
           <Grid DockPanel.Dock="Bottom">
            <Grid.RowDefinitions>
             <RowDefinition />
             <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
             <ColumnDefinition />
             <ColumnDefinition />
             <ColumnDefinition />
             <ColumnDefinition />
             <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <TextBlock Text="{Binding quarter}" x:Name="quarter" Padding="5" Grid.Column="2"  Grid.Row="1" HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="{Binding halfyear}" x:Name="halfyear" Padding="5" Grid.Column="3" Grid.Row="1"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="{Binding year}" x:Name="year" Padding="5" Grid.Column="4"  Grid.Row="1" HorizontalAlignment="Center"  Foreground="#FFB51414" />

            <TextBlock Text="Период: " Padding="5" Grid.Column="1"    HorizontalAlignment="Center"  Foreground="#FFB51414" />

            <TextBlock Text="Квартал" Padding="5" Grid.Column="2"   HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="Полгода" Padding="5" Grid.Column="3"   HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="Год" Padding="5" Grid.Column="4"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
            <TextBlock Text="Цена: " Padding="5" Grid.Row="1"  Grid.Column="1"  HorizontalAlignment="Center"  Foreground="#FFB51414" />
           </Grid>
           <Grid DockPanel.Dock="Top"  >
            <Image Source="{Binding photo}"/>
           </Grid>
          </DockPanel>
         </Button.Content>
        </Button>
        <Button DockPanel.Dock="Bottom" x:Name="send_mess" Tag="{Binding id_Person}" Click="send_mess_Click" Content="Написать сопровождающему"/>
        <DockPanel DockPanel.Dock="Bottom" HorizontalAlignment="Center" Tag="{Binding id_Person}" >
         <ComboBox VerticalAlignment="Top" x:Name="combobox_term">
          <ComboBoxItem Content="Квартал" Tag="1"/>
          <ComboBoxItem Content="Полгода" Tag="2"/>
          <ComboBoxItem Content="Год" Tag="3" IsSelected="True"/>
         </ComboBox>
         <CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" IsChecked="{Binding id_Person, Converter={StaticResource CheckBoxToCheked}}" Content="Выбрать" />
        </DockPanel>

       </DockPanel>
      </GroupBox>
     </DataTemplate>
    </ListBox.ItemTemplate>
   </ListBox>
  </Grid>
 </Border>
</ScrollViewer>

这一切都是因为图片而发生的,怎么打呢?

listbox和groupbox - 如果需要我可以提供他们的样式

编辑:

父网格

<ScrollViewer VerticalScrollBarVisibility="Auto" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" x:Name="gridrtfbox" Visibility="Collapsed">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                </Grid>
                <Grid Grid.Row="1">...

1 个答案:

答案 0 :(得分:0)

我使用转换

<GroupBox Header="Сопровождающие" Width="300" HorizontalAlignment="Stretch" x:Name="GroupBox" Background="Transparent" Height="{Binding ElementName=list_category, Converter={StaticResource ConSetHeigthKey}}">

转换器:

public class ConSetHeigthTrainer : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter,
                                  CultureInfo culture)
            {
                try
                {
                    return (value as System.Windows.Controls.ListBox).ActualHeight-20;
                }
                catch (Exception Exception) { return (value as System.Windows.Controls.ListBox).ActualHeight - 20; }
            }

            public object ConvertBack(object value, Type targetType, object parameter,
                                      CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }