【问题标题】:How to set the Grid.column value in the style datatrigger in WPF如何在 WPF 中的样式数据触发器中设置 Grid.column 值
【发布时间】:2018-03-06 13:46:09
【问题描述】:

我在行 =o 和列 =1 中有一个网格 单击按钮时,我想更改此 grid.row =0 和 grid.column=0。 我已经尝试通过创建带有数据触发器的样式来设置 grod.column = 0。因为这是 gris 的本地值,这在样式属性中不可用。Grid.column 的值没有改变。您能帮我如何更改 xaml 中的 grid.column 值以满足我的要求。

【问题讨论】:

  • “我已经尝试过...” - 显示您尝试过的内容。我相信你快到了,但问题是different
  • 请添加您尝试过的实际代码,以便我们对其进行审核。
  • 另外,请花点时间阅读How to Ask 主题。这将帮助您提出广受好评的问题,从而更有机会快速获得答案。

标签: c# wpf xaml


【解决方案1】:

更改列就像更改附加属性的值一样简单。

以下是ToggleButton 在第一列和第二列之间移动的示例,具体取决于IsChecked 值:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <ToggleButton>
        <ToggleButton.Style>
            <Style>
                <!-- <Setter Property="Grid.Column" Value="0" /> optional, see below for explanation -->
                <Style.Triggers>
                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Setter Property="Grid.Column" Value="1" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>
</Grid>

演示:

是否使用DataTrigger 无关紧要。

在处理依赖属性时注意value precedence,例如在本地设置值将使触发器无用(您将不得不使用另一种方法,例如StoryBoard 来覆盖它):

<ToggleButton Grid.Column="0"> <!-- trigger will not be able to change it -->
...

样式设置器是设置初始值的正确位置(请参阅上面第一个 xaml 中的注释部分)。

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 2011-09-30
    • 2018-01-28
    • 2012-03-14
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多