[英]Binding width to a columnWidth of a grid that is not visible in XAML
im trying to bind the width of a button and a TextBox to the width of a column they are in. Additionally i want to change the visibility of the whole grid. 我试图将按钮和文本框的宽度绑定到它们所在的列的宽度。此外,我想更改整个网格的可见性。 I set visibility to "Collapsed" and in the code behind file i change the visibility to visible 我将可见性设置为“崩溃”,并在文件后面的代码中将可见性更改为“可见”
C#: C#:
Grid1.Visibility = Visibility.Visible;
The TextBlocks in the Grid are visible, but the Textbox and the button are not, i guess the width is 0, because of the grid being invisible. 网格中的TextBlocks可见,但是Textbox和按钮不可见,我猜宽度为0,因为网格不可见。 Whats the best way to tell the button to update the width after changing the visibility to visible? 在将可见性更改为可见后,告诉按钮更新宽度的最佳方法是什么?
This is the xaml code 这是xaml代码
<Grid x:Name="Grid1" Margin="0,100,0,0" Visibility="Collapsed"> <!--HERE -->
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="Column0" Width="100"/>
<ColumnDefinition x:Name="Column1" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="60"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding MaterialName, Mode=OneWay}" VerticalAlignment="Center" FontSize="40"/>
<TextBlock Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" Text="Anzahl:" FontSize="30"/>
<TextBox
Grid.Column="1"
Grid.Row="1"
x:Name="QuantityTextBox"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Height="72" TextWrapping="Wrap"
Text="{Binding Quantity, Mode=TwoWay}"
Width="{Binding ElementName=Column1, Path=ActualWidth}"/><!-- HERE -->
<Button
Grid.Column="1"
Grid.Row="2"
Width="{Binding ElementName=Column1, Path=ActualWidth}"
Content="Content1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
x:Name="ButtonName"
Click="Button_Click"/>
</Grid>
If I understand correctly, you want the TextBox and Button to Stretch to the Width of the Grid.Column they are in. If so, the HorizontalAlignment property on your TextBox and Button are causing the problem. 如果我理解正确,则希望TextBox和Button拉伸到它们所在的Grid.Column的宽度。如果是这样,则TextBox和Button的HorizontalAlignment属性会引起问题。 The default HoriziontalAlignment is Stretch so you shouldn't need to define any HorizontalAlignment (or Width) and the controls will size to fit available width. 默认的HoriziontalAlignment是Stretch,因此您不需要定义任何HorizontalAlignment(或Width),控件的大小将适合可用宽度。 Try this: 尝试这个:
<TextBox
Grid.Column="1"
Grid.Row="1"
x:Name="QuantityTextBox"
VerticalAlignment="Center"
Height="72" TextWrapping="Wrap"
Text="{Binding Quantity, Mode=TwoWay}" ><!-- HERE -->
<Button
Grid.Column="1"
Grid.Row="2"
Content="Content1"
VerticalAlignment="Top"
x:Name="ButtonName"
Click="Button_Click"/>
It's also helpful to set ShowGridLines="True" on your Grid when dealing with layout issues. 处理布局问题时,在网格上设置ShowGridLines =“ True”也很有帮助。
I don't think that your problem is Visibility
related (or at least just Visibility
related). 我不认为你的问题是Visibility
相关(或至少只是 Visibility
有关)。 I just remembered that you can't directly data bind a ColumnDefinition.Width
property to a Button.Width
property because they are of different types. 我只是记得您不能直接将ColumnDefinition.Width
属性绑定到Button.Width
属性,因为它们的类型不同。 The Button.Width
property is obviously of type double
, but the ColumnDefinition.Width
property is actually of type GridLength
. Button.Width
属性显然是double
类型的,但ColumnDefinition.Width
属性实际上是GridLength
类型的。
Instead, you would have to data bind between the two using some sort of IValueConverter
. 相反,您必须使用某种IValueConverter
在两者之间进行数据IValueConverter
。 I just found one that you could use from the answer to the How do I databind a ColumnDefinition's Width or RowDefinition's Height? 我刚刚找到了一个可以从如何对ColumnDefinition的Width或RowDefinition的Height进行数据绑定的答案中使用的答案。 question that you could use. 您可以使用的问题。
Of course, once you have done that, you might still have problems because of the Visibility
issue, so we might have to return to that later. 当然,完成此操作后,由于Visibility
问题,您可能仍然会遇到问题,因此我们可能必须稍后再返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.