简体   繁体   English

如何在XAML中将2个网格列的宽度绑定在一起

[英]How to bind 2 grid column widths together in xaml

I'm hoping someone can make this easy. 我希望有人可以简化这一过程。

I have a grid with 5 columns, columns 1 and 3 contain splitters. 我有一个包含5列的网格,第1列和第3列包含分隔符。 What I want to happen is when I move one splitter have the other one move to match such that the effect is the first and last columns being sized together. 我想发生的事情是,当我移动一个拆分器而另一个移动以匹配时,效果是将第一列和最后一列的大小放在一起。

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" x:Name="col1"/>
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}" />
    </Grid.ColumnDefinitions>

    <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" Background="Red"/>
    <GridSplitter Grid.Column="3" HorizontalAlignment="Stretch" Background="Red"/>
</Grid>

Needless to say the above doesn't work. 毋庸置疑,上述方法无效。 And even if it did it would only allow for using the first splitter not the second. 即使这样做,也只能使用第一个分离器,而不能使用第二个。 Can anyone shed some light? 谁能阐明一些想法?

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" x:Name="col1"/>
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="5" />
        <ColumnDefinition Width="{Binding ElementName=helperGrid, Path=ActualWidth}" />
    </Grid.ColumnDefinitions>

    <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" Background="Red"/>
    <GridSplitter Grid.Column="3" HorizontalAlignment="Stretch" Background="Red"/>
    <Grid Grid.Column="0" x:Name="helperGrid" Width="{Binding ElementName=helperGrid2, Path=ActualWidth}" MinWidth="100"/>
    <Grid Grid.Column="4" x:Name="helperGrid2"/>
</Grid>

This worked for me, I changed the first column's width to auto and used two helper grids in the first and last columns 这对我有用,我将第一列的宽度更改为auto,并在第一列和最后一列中使用了两个辅助网格

After much playing around I could get close but not completely what I was looking for. 经过大量的玩耍,我可以接近但并不完全是我想要的东西。 So I ended up placing a Grid in each if the splitter columns instead of GridSplitters and handling the PreviewMouseDown event on the Grid and also the PreviewMouseUp and PreviewMouseMove events of the parent control to implement my own resizing. 因此,我最终在每个拆分器列(而不是GridSplitters)中放置一个Grid,并处理了Grid上的PreviewMouseDown事件以及父控件的PreviewMouseUp和PreviewMouseMove事件,以实现我自己的调整大小。 This was used to detect dragging in columns 1 and 3 and resize colunmns 0 and 3 together. 这用于检测第1列和第3列中的拖动并一起调整第0列和第3列的大小。 Sadly the code has become rather interwoven with the rest of my control so isn't easy to post. 可悲的是,代码已经与我的其余控件交织在一起,因此发布起来并不容易。 Maybe one day when my xaml improves I'll be able to break the functionality out into a properly reusable control. 也许有一天,当我的xaml有所改进时,我就可以将功能分解为适当的可重用控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM