Silverlight 2 中使用 C# 的动态网格

发布于 2024-07-13 22:34:05 字数 834 浏览 5 评论 0原文

我需要将 UIElement 插入到运行时才生成的网格中。 更具体地说,在确定需要显示多少个元素后,我需要将 UIElements 添加到我创建的 RowDefinitions 中。 有没有办法像 C# 中的对象的 XAML 一样控制 Grid.Row、Grid.Column 和 Grid.RowSpan? 如果我的做法是错误的,请告诉我。 我无法使用 StackPanel(我正在创建一个动态手风琴面板,它与动画混淆)。

现在发生的情况是,我在运行时生成 RowDefinition 的数量,并将 UIElement 添加为子元素。 这是行不通的,所有 UIElements 最终都位于彼此之上的第一行。

这是我正在尝试的一个例子:

public partial class Page : UserControl
{
    string[] _names = new string[] { "one", "two", "three" };
    public Page()
    {
        InitializeComponent();
        BuildGrid();
    }
    public void BuildGrid()
    {
        LayoutRoot.ShowGridLines = true;
        foreach (string s in _names)
        {
            LayoutRoot.RowDefinitions.Add(new RowDefinition());
            LayoutRoot.Children.Add(new Button());
        }
    }
}

谢谢!

I need to insert UIElements into a Grid that does not get generated until runtime. More specifically, I need to add UIElements to the RowDefinitions I create after I determine how many elements need to be displayed. Is there a way to contorl the Grid.Row and Grid.Column and Grid.RowSpan like in XAML for objects in C#? If I am going about this wrong, please let me know. I can not use a StackPanel (I am creating an dynamic accordian panel and it messes with the animation).

Right now what happens is that I generate the number of RowDefinitions at runtime and add UIElements as the children. This isn't working, all the UIElements end up in the first row layered on top of each other.

Here is an example of what I am trying:

public partial class Page : UserControl
{
    string[] _names = new string[] { "one", "two", "three" };
    public Page()
    {
        InitializeComponent();
        BuildGrid();
    }
    public void BuildGrid()
    {
        LayoutRoot.ShowGridLines = true;
        foreach (string s in _names)
        {
            LayoutRoot.RowDefinitions.Add(new RowDefinition());
            LayoutRoot.Children.Add(new Button());
        }
    }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑,眼淚并存 2024-07-20 22:34:05

除了网格并不是真正适合这项工作的工具(ListView 是)之外,您还需要告诉按钮它属于哪一行。

public void BuildGrid()
{
    LayoutRoot.ShowGridLines = true;
    int rowIndex = 0;
    foreach (string s in _names)
    {
        LayoutRoot.RowDefinitions.Add(new RowDefinition());
        var btn = new Button()
        LayoutRoot.Children.Add(btn);
        Grid.SetRow(btn, rowIndex);
        rowIndex += 1;
    }
}

Apart from the Grid not really being the right tool for the job (a ListView would be) you need to tell the Button which row it belongs to.

public void BuildGrid()
{
    LayoutRoot.ShowGridLines = true;
    int rowIndex = 0;
    foreach (string s in _names)
    {
        LayoutRoot.RowDefinitions.Add(new RowDefinition());
        var btn = new Button()
        LayoutRoot.Children.Add(btn);
        Grid.SetRow(btn, rowIndex);
        rowIndex += 1;
    }
}
夏末 2024-07-20 22:34:05

执行您要查找的操作的最佳方法是遵循以下模式:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid x:Name="DataGridTest" />
    </Grid>
</UserControl>

Page.xaml.cs:

public partial class Page : UserControl
    {
        string[] _names = new string[] { "one", "two", "three" };

        public Page()
        {
            InitializeComponent();
            BuildGrid();
        }

        public void BuildGrid()
        {
            DataGridTest.ItemsSource = _names;
        }
    }

这会根据字符串数组的内容动态构建行。 将来更好的方法是使用 ObservableCollection< /a> 其中 T 实现 INotifyPropertyChanged。 如果您从集合中删除或添加项目以及 T 的属性发生更改,这将通知 DataGrid 更新其行。

要进一步自定义用于显示内容的 UIElements,您可以使用 DataGridTemplateColumn

<data:DataGridTemplateColumn Header="Symbol">
    <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding PutNameOfPropertyHere}" />
        </DataTemplate>
    </data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>

The best way to do what you're looking for is to follow the pattern below:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid x:Name="DataGridTest" />
    </Grid>
</UserControl>

Page.xaml.cs:

public partial class Page : UserControl
    {
        string[] _names = new string[] { "one", "two", "three" };

        public Page()
        {
            InitializeComponent();
            BuildGrid();
        }

        public void BuildGrid()
        {
            DataGridTest.ItemsSource = _names;
        }
    }

This builds the rows dynamically from the contents of your string array. In future an even better way would be to use an ObservableCollection where T implements INotifyPropertyChanged. This will notify the DataGrid to update it's rows if you remove or add an item from the collection and also when properties of T change.

To further customize the UIElements used to display things you can use a DataGridTemplateColumn:

<data:DataGridTemplateColumn Header="Symbol">
    <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding PutNameOfPropertyHere}" />
        </DataTemplate>
    </data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
何处潇湘 2024-07-20 22:34:05

我从未使用过这些东西,但是您不应该将 Button 添加到 RowDefinitions 而不是 Children 中吗? (只是一个逻辑观察)

I have never used this stuff, but shouldn't you be adding the Button to the RowDefinitions, instead of the Children? (just a logical observation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文