【问题标题】:Get Grid.Column property from sender从发件人处获取 Grid.Column 属性
【发布时间】:2010-11-28 10:54:10
【问题描述】:

我对 WP/Visual C# 很陌生,我目前正在制作一个项目,该项目涉及一个填充了 100 多个按钮的网格(带有列和行),我已经为所有这些按钮创建了一个通用函数,但我确实需要为了知道实际按下的是哪一个,我找出了按钮中的“Grid.Column”“Grid.Row”属性,然后我用它们将每个按钮放置在正确的位置。

属性:

<Button Content="" Margin="-12,-12,0,-13" Width="69" HorizontalAlignment="Left" BorderThickness="2" Background="{x:Null}" Padding="0" Grid.Row="1" Grid.Column="1"/>

但我不能做的是从我的按钮代码中获取这些属性,我试过这个:

int rows = ((Button)sender).Grid.rows;

这给了我这个错误:

Error 1: 'System.Windows.Controls.Button' does not contain a definition for 'Grid' and no extension method 'Grid' accepting a first argument of type 'System.Windows.Controls.Button' could be found (are you missing a using directive or an assembly reference?)  C:\Users\K\Documents\Visual Studio 2010\Projects\buscaminas\bus\MainPage.xaml.cs    71

有什么解决办法吗? :D

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    它被称为附加依赖属性。您需要使用声明它们可以访问的类。

    var row = Grid.GetRow((Button)sender);
    var col = Grid.GetColumn((Button)sender);
    

    【讨论】:

    • 还有一件事,我应该怎么做才能为该特定按钮设置一个属性?
    • 如果您的意思是网格附加属性,如 Row、Column、RowSpan、ColumnSpan,它们都有 setter 方法。 Grid.SetXXX(FrameworkElement, value)
    • 不是真的:S,我想更改例如在 row=0, col=0 的特定网格中包含的按钮的“内容”属性
    • 在这种情况下,您需要使用 VisualTreeHelper 类来发现网格中的所有子项,然后找到具有所需行/列的一个,将其转换为按钮并设置按钮内容
    猜你喜欢
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多