2009-01-20 28 views
38

我有一个窗口与我的用户控件,我想使用户控件宽度等于窗口宽度。怎么做?如何在宽度上将WPF用户控件拉伸到其窗口?

用户控制是水平菜单和包含具有三列的网格:

<ColumnDefinition Name="LeftSideMenu" Width="433"/> 
<ColumnDefinition Name="Middle" Width="*"/> 
<ColumnDefinition Name="RightSideMenu" Width="90"/> 

就是这个原因我想要的窗口宽度,以拉伸用户控制到100%的宽度,与第二列相对的。

编辑:

我使用的是网格,有一个窗口的代码:

<Window x:Class="TCI.Indexer.UI.Operacao" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles" 
    Title=" " MinHeight="550" MinWidth="675" Loaded="Load" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized" Focusable="True" 
    x:Name="windowOperacao"> 
    <Canvas x:Name="canv"> 
     <Grid> 
      <tci:Status x:Name="ucStatus"/> <!-- the control which I want to stretch in width --> 
     </Grid> 
    </Canvas> 
</Window> 

回答

38

您需要确保您的usercontrol未在usercontrol的xaml文件中设置宽度。只需从中删除宽度=“...”,你就可以走了!

编辑:这是我测试过它的代码:

SOUserAnswerTest.xaml:

<UserControl x:Class="WpfApplication1.SOAnswerTest" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Name="LeftSideMenu" Width="100"/> 
      <ColumnDefinition Name="Middle" Width="*"/> 
      <ColumnDefinition Name="RightSideMenu" Width="90"/> 
     </Grid.ColumnDefinitions> 
     <TextBlock Grid.Column="0">a</TextBlock> 
     <TextBlock Grid.Column="1">b</TextBlock> 
     <TextBlock Grid.Column="2">c</TextBlock> 
    </Grid> 
</UserControl> 

Window1.xaml:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1" 
    Title="Window1" Height="300" Width="415"> 
    <Grid> 

     <local:SOAnswerTest Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="2"/> 
    </Grid> 
</Window> 
+0

感谢您的关注,阿尔扬。它在这里也可以正常工作,但是我的窗口没有设置Width =“415”,因为它被设计为全屏。当我设定宽度时,效果很好。 – 2009-01-20 19:43:21

1

什么容器要补充的用户控件来?通常,当您将控件添加到网格时,它们将伸展以填充可用空间(除非它们的行/列被限制到特定宽度)。

2

是否将Horizo​​ntalAlignment设置为Stretch,并将用户控件上的Width设置为Auto以获得所需的结果?

7

是画布你的窗口至关重要?如果没有,请尝试删除它并将Grid作为主面板。除非指定画布,否则画布没有大小,而网格通常占用所有可用空间。在画布内部,网格将没有可用空间。

20

WPF中的Canvas不提供太多的自动布局支持。由于这个原因(Horizo​​ntalAlignment和VerticalAlignment不能像预期的那样工作),我尝试避开它们,但是我得到了代码来处理这些小修改(将控件的宽度和高度绑定到画布的ActualWidth/ActualHeight)。

<Window x:Class="TCI.Indexer.UI.Operacao" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles" 
Title=" " MinHeight="550" MinWidth="675" Loaded="Load" 
ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen" 
WindowState="Maximized" Focusable="True" x:Name="windowOperacao"> 

<Canvas x:Name="canv"> 
    <Grid> 
     <tci:Status x:Name="ucStatus" Width="{Binding ElementName=canv 
                , Path=ActualWidth}" 
             Height="{Binding ElementName=canv 
                , Path=ActualHeight}"/> 
     <!-- the control which I want to stretch in width --> 
    </Grid> 
</Canvas> 

画布是这里的问题。如果你实际上并没有利用画布提供的布局或Z-Order“挤压”功能(想想PhotoShop中的flatten命令),我会考虑使用像Grid这样的控件来代替,所以你最终不会不得不学习控制的怪癖,这种控制与WPF所期望的不同。

+0

谢谢,绑定到ActualWidth帮助我在一个项目中,我有一个自定义UserControl在StackPanel。 StackPanel使用Horizo​​ntalAlignement = Stretch并占据整个宽度,但usercontrol保持在0宽度。将用户控件绑定到它的父堆栈面板ActualWidth为我解决了它。 – 2010-10-31 15:44:35

1

改为在用户控件中使用宽度和高度,请使用MinHeight和MinWidth。 然后你可以很好地配置UC,并且能够在其他窗口内拉伸。

好吧,就像我在WPF中看到的一样,微软对Windows属性和行为做了一个重新思考,但到目前为止,我没有错过任何来自旧窗口窗体的东西,WPF中的控件在那里,但在一个新的点看法。

0

这对我有效。 不要为UserControl分配任何宽度或高度,并在父窗口中定义行和列定义。

<UserControl x:Class="MySampleApp.myUC" 
     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" 
     mc:Ignorable="d" 
     > 
    <Grid> 

    </Grid> 
</UserControl> 


<Window xmlns:MySampleApp="clr-namespace:MySampleApp" x:Class="MySampleApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="auto" Width="auto" MinWidth="1000" > 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" />   
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" />    
    </Grid.ColumnDefinitions> 
    <MySampleApp:myUC Grid.Column="0" Grid.Row="0" />  
</Grid> 

相关问题