动态设置MinWidth [英] Dynamic setting MinWidth

查看:80
本文介绍了动态设置MinWidth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个容器,里面有一个Grid.我想以编程方式将网格的最小宽度(grid.MinWidth)绑定到容器的宽度.

Hi,
i have a container that inside it there is a Grid.i want to bind the minimum width(grid.MinWidth) of the grid to the containers width programmatically. how can i do that?

推荐答案

您可以绑定到容器的实际宽度:
You can bind to the actual width of the container:
<Grid Name="myContainer" Width="200">
    <Grid MinWidth="{Binding ElementName=myContainer, Path=ActualWidth}"></Grid>
</Grid>



这是您从后面的代码中执行的方法.首先,您的XAML可能是这样的:



Here is how you do it from the code behind. First, here is what your XAML might be:

<Grid Name="myContainer">
    <Grid Name="myGrid" />
</Grid>


您的构造函数可能如下所示:


Your constructor might look like this:

public MainWindow()
{
    // Boilerplate.
    InitializeComponent();

    // Programmatic binding.
    Binding minWidthBinding = new Binding();
    minWidthBinding.Path = new PropertyPath(Grid.ActualWidthProperty);
    minWidthBinding.ElementName = "myContainer";
    BindingOperations.SetBinding(myGrid, Grid.WidthProperty, minWidthBinding);
}


这篇关于动态设置MinWidth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆