WPF SharedSizeGroup GridSplitter问题 [英] WPF SharedSizeGroup GridSplitter Issue

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

问题描述

我希望为我的顶层布局使用网格。网格将有1列和n行。网格中的每一行还应包含一个网格,网格应包含3列和1行。第二列是一个GridSplitter,我试图使用SharedSizeGroup,这样就改变了所有嵌套网格中第一列的大小。



这是什么我有......并且它的工作原理!! ......以及......如果你点击分离器并调整大小而不放开它的作用......但由于某种原因,如果你调整大小并放开鼠标然后尝试调整使用不同的行似乎坚持。



有什么想法?

 <! -  Parent Grid  -  - > 
< Grid Grid.IsSharedSizeScope =True>
< Grid.RowDefinitions>
< RowDefinition>< / RowDefinition>
< RowDefinition>< / RowDefinition>
< /Grid.RowDefinitions>

<! - First Grid - >
< Grid Grid.Row =0>
< Grid.ColumnDefinitions>
< ColumnDefinition SharedSizeGroup =AWidth =Auto>< / ColumnDefinition>
< ColumnDefinition SharedSizeGroup =BWidth =Auto>< / ColumnDefinition>
< ColumnDefinition SharedSizeGroup =CWidth =Auto>< / ColumnDefinition>
< /Grid.ColumnDefinitions>

< Label Grid.Column =0>一个左< / Label>
< GridSplitter Grid.Column =1Width =5Background =DarkGray>< / GridSplitter>
< Label Grid.Column =2>单右< / Label>
< / Grid>

<! - 第二个网格 - >
< Grid Grid.Row =1>
< Grid.ColumnDefinitions>
< ColumnDefinition SharedSizeGroup =AWidth =Auto>< / ColumnDefinition>
< ColumnDefinition SharedSizeGroup =BWidth =Auto>< / ColumnDefinition>
< ColumnDefinition SharedSizeGroup =CWidth =Auto>< / ColumnDefinition>
< /Grid.ColumnDefinitions>

< Label Grid.Column =0>双左< / Label>
< GridSplitter Grid.Column =1Width =5Background =DarkGray>< / GridSplitter>
< Label Grid.Column =2>双右< / Label>
< / Grid>

< / Grid>


解决方案

重新发布 ms connect

您通常可以解决这通过不使用SharedSizeGroup,而是将所有共享大小绑定到一个对象上的单个属性(例如,您的datacontext):

 < Window xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:my =clr-namespace:WpfApplication3
Height =350Width =525Title =MainWindow>

< Window.DataContext>
< my:MainWindowData Width0 =1 *Width1 =1 */>
< /Window.DataContext>

< Window.Resources>

< DataTemplate x:Key =dt>
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width ={Binding Path = Width0,Mode = TwoWay}/>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width ={Binding Path = Width1,Mode = TwoWay}/>
< /Grid.ColumnDefinitions>
< Button Grid.Column =0Content ={Binding Width0}/>
< GridSplitter Grid.Column =1Width =10ResizeBehavior =PreviousAndNextResizeDirection =Columns/>
< Button Grid.Column =2Content ={Binding Width1}/>
< / Grid>
< / DataTemplate>

< /Window.Resources>

< StackPanel>
< ContentPresenter Content ={Binding}ContentTemplate ={StaticResource dt}/>
< ContentPresenter Content ={Binding}ContentTemplate ={StaticResource dt}/>
< / StackPanel>

< / Window>

其中Width0和Width1是匹配类型(GridLength)。它适用于任何组合的任何尺寸(固定,星形和自动)。
$ b

UPDATE

另外也许更好一些,而不是绑定到DataContext,你可以纯粹在XAML中完成。只需定义一个具有命名列的主栅格(不一定是父项,但需要某种方法来引用它),然后通过名称绑定到它们。

 < Window xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006 / xaml
Height =350Width =525Title =MainWindow>

<! - 共享大小仅适用于固定大小的列,因此安全 - >
<! - 或者你可以硬编码分隔符列的宽度 - >
< Grid Name =masterGridGrid.IsSharedSizeScope =True>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =1 *Name =masterColumn0/>
< ColumnDefinition Width =AutoSharedSizeGroup =masterColumn1/>
< ColumnDefinition Width =1 *Name =masterColumn2/>
< /Grid.ColumnDefinitions>
< StackPanel Grid.ColumnSpan =3>
< StackPanel.Resources>
< DataTemplate x:Key =dt>
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width ={Binding Path = Width,Mode = TwoWay,ElementName = masterColumn0}/>
< ColumnDefinition Width =AutoSharedSizeGroup =masterColumn1/>
< ColumnDefinition Width ={Binding Path = Width,Mode = TwoWay,ElementName = masterColumn2}/>
< /Grid.ColumnDefinitions>
< Button Grid.Column =0Content ={Binding Path = Width,Mode = TwoWay,ElementName = masterColumn0}/>
< Button Grid.Column =2Content ={Binding Path = Width,Mode = TwoWay,ElementName = masterColumn2}/>
< / Grid>
< / DataTemplate>
< ContentPresenter ContentTemplate ={StaticResource dt}/>
< ContentPresenter ContentTemplate ={StaticResource dt}/>
< / StackPanel>
< GridSplitter Grid.Column =1Width =10ResizeBehavior =PreviousAndNextResizeDirection =ColumnsShowsPreview =True/>
< / Grid>

< / Window>

这增加了使用所有网格共享的单个网格分离器的好处。


I wish to use a Grid for my top level layout. The Grid will have 1 column and n rows. Each row in the Grid should also contain a Grid which shall have 3 columns and 1 row. In the second column is a GridSplitter and I am trying to use a SharedSizeGroup so that this changes the size of the first column across all of the nested Grids.

Here is what i have...and it works!!...well kind of...if you click the splitter and resize without letting go it works...but for some reason if you resize something and let go of the mouse and then attempt to resize using a different row it seems to "stick".

Any ideas?

<!-- Parent Grid -->
<Grid Grid.IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <!-- First Grid -->
    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="A" Width="Auto"></ColumnDefinition>
            <ColumnDefinition SharedSizeGroup="B" Width="Auto"></ColumnDefinition>
            <ColumnDefinition SharedSizeGroup="C" Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0">One-Left</Label>
        <GridSplitter Grid.Column="1" Width="5" Background="DarkGray"></GridSplitter>
        <Label Grid.Column="2">One-Right</Label>
    </Grid>

    <!-- Second Grid -->
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="A" Width="Auto"></ColumnDefinition>
            <ColumnDefinition SharedSizeGroup="B" Width="Auto"></ColumnDefinition>
            <ColumnDefinition SharedSizeGroup="C" Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0">Two-Left</Label>
        <GridSplitter Grid.Column="1" Width="5" Background="DarkGray"></GridSplitter>
        <Label Grid.Column="2">Two-Right</Label>
    </Grid>

</Grid>

解决方案

Reposting my answer from ms connect:

You can usually work around this by not using SharedSizeGroup and instead binding all shared sizes to single property on one object (eg. your datacontext):

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication3"
        Height="350" Width="525" Title="MainWindow">

    <Window.DataContext>
        <my:MainWindowData Width0="1*" Width1="1*" />
    </Window.DataContext>

    <Window.Resources>

        <DataTemplate x:Key="dt">
         <Grid>
            <Grid.ColumnDefinitions>
             <ColumnDefinition Width="{Binding Path=Width0, Mode=TwoWay}" />
             <ColumnDefinition Width="Auto" />
             <ColumnDefinition Width="{Binding Path=Width1, Mode=TwoWay}" />
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Content="{Binding Width0}" />
            <GridSplitter Grid.Column="1" Width="10" ResizeBehavior="PreviousAndNext" ResizeDirection="Columns" />
            <Button Grid.Column="2" Content="{Binding Width1}" />
         </Grid>
        </DataTemplate>

    </Window.Resources>

    <StackPanel>
        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource dt}" />
        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource dt}" />
    </StackPanel>

</Window>

Where Width0 and Width1 are of matching type (GridLength). It works with any kind of sizing (fixed, star and auto) in any combination.

UPDATE:

Alternatively and perhaps better, instead of binding to DataContext, you can do it purely in XAML. Just define a single master grid (not necessarily a parent but you need some way to reference it) with named columns then bind to them by name.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="350" Width="525" Title="MainWindow">

    <!-- shared sizing used only on fixed size columns therefore safe -->
    <!-- alternatively you can hardcode width of splitter column -->
    <Grid Name="masterGrid" Grid.IsSharedSizeScope="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" Name="masterColumn0" />
            <ColumnDefinition Width="Auto" SharedSizeGroup="masterColumn1" />
            <ColumnDefinition Width="1*" Name="masterColumn2" />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.ColumnSpan="3">
            <StackPanel.Resources>
                <DataTemplate x:Key="dt">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="{Binding Path=Width, Mode=TwoWay, ElementName=masterColumn0}" />
                            <ColumnDefinition Width="Auto" SharedSizeGroup="masterColumn1" />
                            <ColumnDefinition Width="{Binding Path=Width, Mode=TwoWay, ElementName=masterColumn2}" />
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="0" Content="{Binding Path=Width, Mode=TwoWay, ElementName=masterColumn0}" />
                        <Button Grid.Column="2" Content="{Binding Path=Width, Mode=TwoWay, ElementName=masterColumn2}" />
                    </Grid>
                </DataTemplate>
            </StackPanel.Resources>
            <ContentPresenter ContentTemplate="{StaticResource dt}" />
            <ContentPresenter ContentTemplate="{StaticResource dt}" />
        </StackPanel>
        <GridSplitter Grid.Column="1" Width="10" ResizeBehavior="PreviousAndNext" ResizeDirection="Columns" ShowsPreview="True" />
    </Grid>

</Window>

This has added benefit of using a single grid splitter shared by all grids.

这篇关于WPF SharedSizeGroup GridSplitter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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