更新时间:2023-12-06 13:25:22
您需要确保你的用户控件尚未确定这是在该用户的XAML文件宽度。只是删除了宽度=...,从它,你是好去。
编辑:这是我测试代码它:
的 SOUserAnswerTest.xaml:的
<用户控件X:类=WpfApplication1.SOAnswerTest
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP ://schemas.microsoft.com/winfx/2006/xaml
HEIGHT =300>
<网格和GT;
< Grid.ColumnDefinitions>
< ColumnDefinition NAME =LeftSideMenuWIDTH =100/>
< ColumnDefinition名称=中WIDTH =*/>
< ColumnDefinition NAME =RightSideMenuWIDTH =90/>
< /Grid.ColumnDefinitions>
将; TextBlock的Grid.Column =0>一种与所述; / TextBlock的>
< TextBlock的Grid.Column =1> B< / TextBlock的>
< TextBlock的Grid.Column =2> C< / TextBlock的>
< /网格和GT;
< /用户控件>
的 Window1.xaml:的
<窗口x:类=WpfApplication1.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:地方=CLR的命名空间:WpfApplication1
标题=窗口1 HEIGHT =300WIDTH =415>
<网格和GT;
<局部:SOAnswerTest Grid.Column =0Grid.Row =5Grid.ColumnSpan =2/>
< /网格和GT;
< /窗GT;
I have a Window with my user control and I would like to make usercontrol width equals window width. How to do that?
The user control is a horizontal menu and contains a grid with three columns:
<ColumnDefinition Name="LeftSideMenu" Width="433"/>
<ColumnDefinition Name="Middle" Width="*"/>
<ColumnDefinition Name="RightSideMenu" Width="90"/>
That is the reason I want the window width, to stretch the user control to 100% width, with the second column relative.
EDIT:
I am using a grid, there is the code for Window:
<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>
You need to make sure your usercontrol hasn't set it's width in the usercontrol's xaml file. Just delete the Width="..." from it and you're good to go!
EDIT: This is the code I tested it with:
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>