根据整个网格调整WPF网格ColumnDefinition的宽度

时间:2016-12-06 20:15:44

标签: c# .net wpf

我的网格看起来像这样:

<Grid>
    <Grid.ColumnDefinitions>
        <!--  Name  -->
        <ColumnDefinition Width="*"/>
        <!--  Separator  -->
        <ColumnDefinition Width="Auto"/>

        <!--  Session Id  -->
        <ColumnDefinition Width=".5*"  MaxWidth="240" />
        <!--  Separator  -->
        <ColumnDefinition Width="Auto" />

        <!--  Bag  -->
        <ColumnDefinition Width="{Binding ElementName=CurrentBagNumber,
                                          Path=Width}" />
        <!--  Bag Status  -->
        <ColumnDefinition Width="{Binding ElementName=BagStatusText,
                                          Path=Width}" />

        <!--  Several more static width columns  -->        
        .....

这会话Id列的大小是Name列的一半。但真正想要的是当网格宽度大约为1024像素时,会话ID为.1和名称一样大。并且当网格宽度大约为2000像素时,会话ID为.9。并且在调整大小时,要在这两者之间进行平滑的调整。

有点像渐变百分比。

因此,回顾一下,当网格很小时,SessionId比Name小得多,但是当网格很大时,它与Name的大小相同。这在WPF中是否可行?

1 个答案:

答案 0 :(得分:0)

开箱即用的WPF网格,不,你问的是不可能的。

但是,您可以使用附加行为来自定义列宽,根据屏幕宽度动态更改ColumnDefinition的Width参数。

const MessagesRoutes: Routes = [
    { path: '', resolve: { messages: InboxResolver }, children: [
        { path: 'inbox', component: InboxComponent},
        { path: 'xxx', component: XxxComponent},
        { path: 'zzz', component: XxxComponent},
    ]}
];

ns =您的行为命名空间

CurrentWidth是绑定到当前窗口/控件的宽度

在MyBehavior的实现中,您有4个附加的依赖项属性,它们将根据SmallWidth / LargeWidth / Threshhold / CurrentWidth手动设置ColumnDefinition Width属性

See here for more information on Attached Properties

相关问题