Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在DataTemplate拉伸中创建最后一个文本框_Wpf_Datatemplate - Fatal编程技术网

Wpf 在DataTemplate拉伸中创建最后一个文本框

Wpf 在DataTemplate拉伸中创建最后一个文本框,wpf,datatemplate,Wpf,Datatemplate,我有一个ItemsControl: <Border Grid.Row="1" Margin="20" BorderBrush="AliceBlue" BorderThickness="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> <ItemsControl Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

我有一个ItemsControl:

<Border Grid.Row="1" Margin="20" BorderBrush="AliceBlue" BorderThickness="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
   <ItemsControl Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding SelectedEventHistoryEntryCollection}" ItemTemplateSelector="{StaticResource computerEventHistoryDataTemplateSelector}"/>
</Border>

使用一些数据模板。我正在测试第一个模板:

<DataTemplate x:Key="DetailsDataTemplate">
        <Grid>
            <Label Width="150" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{x:Static resx:Resources.Label_ServiceDept}"/>
            <TextBox Margin="110,0,0,0" Width="200" IsReadOnly="True" Text="{Binding ServiceDepartment}" VerticalAlignment="Top" HorizontalAlignment="Left"/>

            <Label Width="150" Margin="0,40,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{x:Static resx:Resources.Label_SLA}"/>
            <TextBox Margin="110,40,0,0" Width="200" IsReadOnly="True" Text="{Binding SLA}" VerticalAlignment="Top" HorizontalAlignment="Left"/>

            <Label Width="150" Margin="0,80,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{x:Static resx:Resources.Label_Details}"/>
            <TextBox Margin="110,80,10,10" IsReadOnly="True" TextWrapping="Wrap" Text="{Binding Details}"/>
        </Grid>
    </DataTemplate>

我希望datatemplate中的最后一个文本框使用完剩余的空间,但我尝试的任何方法都不起作用。我怎样才能使这个不可操作的文本框伸展


编辑:删除了文本框上的高度属性。

使用
LastChildFill=“true”
将网格更改为
DockPanel

然后,您可以去掉所有的
边距,让WPF自动进行布局。

使用
LastChildFill=“true”
将网格更改为
DockPanel
然后,您可以去掉所有的
边距
并让WPF自动进行布局。

使用
而不是

DockPanel
中的最后一项使用剩余空间

使用
而不是

DockPanel
中的最后一项使用剩余空间

通常,对于这种布局,我使用
与星形调整*结合使用,而不是使用边距

更新1:(为清晰起见已删除)

更新2:当我遇到这样的情况时,我不知道在哪里应用绑定或模板,我会尝试备份并以不同的方式看待问题。我几乎总是把它带回MVVM模式。在本例中,模型是EventHistory对象。您的ViewModel有一个
可观察集合
。您的视图只是绑定到该集合。所以,要得到这样的结果:

<Grid Background="Transparent">

    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="8" />
        <RowDefinition Height="1.5*" />
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" />

    <GridSplitter Grid.Row="1" Background="Transparent"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />

    <Border Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="3" Padding="8">
        <ContentControl Grid.Row="2" x:Name="myContent" />            
    </Border>

</Grid>
<UserControl x:Class="WpfApplication1.CustomUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Status" />
        <Label Grid.Row="1" Grid.Column="0" Content="Description" />

        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Status}" />
        <TextBox Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Description}" />

    </Grid>
</UserControl>
myContent.Content = new CustomUserControl();

您可以在视图中使用以下内容:

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="8" />
        <RowDefinition Height="1.5*" />
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" AutoGenerateColumns="True" 
                ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" 
                HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" />

    <GridSplitter Grid.Row="1" 
                    Background="Transparent"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"   />

    <Border Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="3" Padding="8">

        <Grid>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Grid.Column="0" Content="Status" />
            <TextBox Grid.Row="0" Grid.Column="1" Margin="0,0,0,8" Text="{Binding Path=Status}" />

            <Label Grid.Row="1" Grid.Column="0" Content="Detailed Description" />
            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}" />

        </Grid>
    </Border>
</Grid>

这很好,因为这就是你想要实现的。屏幕底部2个标签和文本框上的绑定不必是任何数据模板的一部分。它们是视图的一部分(屏幕截图中红色边框内的所有内容)。所有的大小调整工作,一切都很好。如果您真的想将内容移动到DataTemplate中,这可能是可能的,但此时看起来更自然

注意:创建视图(红色边框内的区域)后,我将其放在主窗口中,并根据屏幕截图在右侧留下一个区域。我还使用了网格拆分器、星形调整和边距,这样可以在保持图片比例的同时占用所有可用空间

希望这有帮助

通常,对于这种布局,我使用
与星形调整*结合使用,而不是使用边距

更新1:(为清晰起见已删除)

更新2:当我遇到这样的情况时,我不知道在哪里应用绑定或模板,我会尝试备份并以不同的方式看待问题。我几乎总是把它带回MVVM模式。在本例中,模型是EventHistory对象。您的ViewModel有一个
可观察集合
。您的视图只是绑定到该集合。所以,要得到这样的结果:

<Grid Background="Transparent">

    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="8" />
        <RowDefinition Height="1.5*" />
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" />

    <GridSplitter Grid.Row="1" Background="Transparent"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />

    <Border Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="3" Padding="8">
        <ContentControl Grid.Row="2" x:Name="myContent" />            
    </Border>

</Grid>
<UserControl x:Class="WpfApplication1.CustomUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Status" />
        <Label Grid.Row="1" Grid.Column="0" Content="Description" />

        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Status}" />
        <TextBox Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Description}" />

    </Grid>
</UserControl>
myContent.Content = new CustomUserControl();

您可以在视图中使用以下内容:

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="8" />
        <RowDefinition Height="1.5*" />
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" AutoGenerateColumns="True" 
                ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" 
                HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray" />

    <GridSplitter Grid.Row="1" 
                    Background="Transparent"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"   />

    <Border Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="3" Padding="8">

        <Grid>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Grid.Column="0" Content="Status" />
            <TextBox Grid.Row="0" Grid.Column="1" Margin="0,0,0,8" Text="{Binding Path=Status}" />

            <Label Grid.Row="1" Grid.Column="0" Content="Detailed Description" />
            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=Description}" />

        </Grid>
    </Border>
</Grid>

这很好,因为这就是你想要实现的。屏幕底部2个标签和文本框上的绑定不必是任何数据模板的一部分。它们是视图的一部分(屏幕截图中红色边框内的所有内容)。所有的大小调整工作,一切都很好。如果您真的想将内容移动到DataTemplate中,这可能是可能的,但此时看起来更自然

注意:创建视图(红色边框内的区域)后,我将其放在主窗口中,并根据屏幕截图在右侧留下一个区域。我还使用了网格拆分器、星形调整和边距,这样可以在保持图片比例的同时占用所有可用空间


希望这有帮助

我对第一个答案的理解有点慢。在意识到你在追求什么之后,我认为这种方法是不正确的。另外,我认为在使用数据模板之后,您无法轻松实现您的目标。但是,我认为您有几个选择:

  • 检查一下,因为is擅长构建复合应用程序。然而,对于这个问题来说,这似乎有点过头了。因此,更直接的方法可能是
  • 为每个单独的详图视图构建自定义控件,然后根据需要编写一些自定义逻辑来加载每个视图。你会这样设置它
  • 主网格和详细视图主机(即ContentControl)的设置如下:

    <Grid Background="Transparent">
    
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="8" />
            <RowDefinition Height="1.5*" />
        </Grid.RowDefinitions>
    
        <DataGrid Grid.Row="0" />
    
        <GridSplitter Grid.Row="1" Background="Transparent"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
    
        <Border Grid.Row="2" BorderBrush="DarkGray" BorderThickness="1" CornerRadius="3" Padding="8">
            <ContentControl Grid.Row="2" x:Name="myContent" />            
        </Border>
    
    </Grid>
    
    <UserControl x:Class="WpfApplication1.CustomUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
    
            <Label Grid.Row="0" Grid.Column="0" Content="Status" />
            <Label Grid.Row="1" Grid.Column="0" Content="Description" />
    
            <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Status}" />
            <TextBox Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Description}" />
    
        </Grid>
    </UserControl>
    
    myContent.Content = new CustomUserControl();
    
    您的每个自定义控件都必须使用星形大小等,以使布局看起来正确-这就是您的问题所在。显然,仍有大量的接线工作需要完成


    那会给你你想要的。很抱歉在第一个答案上绕来绕去

    我对第一个答案的理解有点慢。在意识到你在追求什么之后,我认为这种方法是不正确的。另外,我认为在使用数据模板之后,您无法轻松实现您的目标。然而,我认为你有一个f