Wpf ItemsControl在主窗口';s构造函数

Wpf ItemsControl在主窗口';s构造函数,wpf,itemscontrol,mainwindow,visualtreehelper,itemspaneltemplate,Wpf,Itemscontrol,Mainwindow,Visualtreehelper,Itemspaneltemplate,根据对问题“”的回答,我有以下几点: 那么,在窗口显示给用户之前,我如何设置网格的行集合和列集合呢?WPF在后台为ItemsControl的项(通常是数据模板)生成“容器”,它们不会立即可用 了解项目何时可用的唯一方法是订阅ItemsControl的ItemContainerGenerator属性上的StatusChanged事件: itemsControl1.ItemContainerGenerator.StatusChanged += ic_GeneratorStatusChanged;

根据对问题“”的回答,我有以下几点:

那么,在窗口显示给用户之前,我如何设置网格的行集合和列集合呢?

WPF在后台为ItemsControl的项(通常是数据模板)生成“容器”,它们不会立即可用

了解项目何时可用的唯一方法是订阅ItemsControl的ItemContainerGenerator属性上的StatusChanged事件:

itemsControl1.ItemContainerGenerator.StatusChanged += ic_GeneratorStatusChanged;
。。。然后从事件处理程序中取消订阅,因为您只需要它触发一次:

void ic_GeneratorStatusChanged(object sender, EventArgs e)
{

    if (itemsControl1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
        return;

    itemsControl1.ItemContainerGenerator.StatusChanged -= ic_GeneratorStatusChanged;

    // your items are now generated
}

当然,这是一种迂回的处理方式,但这是知道ItemsControl的项存在于可视化树中的唯一方法。

我厌倦了为网格编写
行定义和
列定义,因此,创建了一些自定义DependencyProperties,用于指定网格定义中的行/列数

可以找到依赖项属性的代码,其使用方式如下:

<Grid local:GridHelpers.RowCount="{Binding RowCount}"
      local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
从属属性

public class GridProperties
{
    #region RowCount Property

    /// <summary>
    /// Adds the specified number of Rows to RowDefinitions. Default Height is Auto
    /// </summary>
    public static readonly DependencyProperty RowCountProperty =
        DependencyProperty.RegisterAttached("RowCount", typeof(int),
        typeof(GridProperties),
        new PropertyMetadata(-1, RowCountChanged));

    // Get
    public static int GetRowCount(DependencyObject obj)
    {
        return (int)obj.GetValue(RowCountProperty);
    }

    // Set
    public static void SetRowCount(DependencyObject obj, int value)
    {
        obj.SetValue(RowCountProperty, value);
    }

    // Change Event - Adds the Rows
    public static void RowCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || (int)e.NewValue < 0)
            return;

        Grid grid = (Grid)obj;
        grid.RowDefinitions.Clear();

        for (int i = 0; i < (int)e.NewValue; i++)
            grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

        //SetStarRows(grid);
    }

    #endregion

    #region ColumnCount Property

    /// <summary>
    /// Adds the specified number of Columns to ColumnDefinitions. Default Width is Auto
    /// </summary>
    public static readonly DependencyProperty ColumnCountProperty =
        DependencyProperty.RegisterAttached("ColumnCount", typeof(int),
        typeof(GridProperties),
        new PropertyMetadata(-1, ColumnCountChanged));

    // Get
    public static int GetColumnCount(DependencyObject obj)
    {
        return (int)obj.GetValue(ColumnCountProperty);
    }

    // Set
    public static void SetColumnCount(DependencyObject obj, int value)
    {
        obj.SetValue(ColumnCountProperty, value);
    }

    // Change Event - Add the Columns
    public static void ColumnCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || (int)e.NewValue < 0)
            return;

        Grid grid = (Grid)obj;
        grid.ColumnDefinitions.Clear();

        for (int i = 0; i < (int)e.NewValue; i++)
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });

        // SetStarColumns(grid);
    }

    #endregion
}
公共类GridProperties
{
#区域行计数属性
/// 
///将指定数量的行添加到行定义中。默认高度为“自动”
/// 
公共静态只读DependencyProperty RowCountProperty=
DependencyProperty.RegisterAttached(“行计数”,类型为(int),
类型(网格属性),
新属性元数据(-1,RowCountChanged));
//得到
公共静态int GetRowCount(DependencyObject obj)
{
返回(int)对象GetValue(RowCountProperty);
}
//设置
公共静态void SetRowCount(DependencyObject对象,int值)
{
对象设置值(RowCountProperty,value);
}
//更改事件-添加行
公共静态void RowCountChanged(DependencyObject对象、DependencyPropertyChangedEventArgs e)
{
如果(!(对象是网格)| |(int)e.NewValue<0)
返回;
网格网格=(网格)obj;
grid.RowDefinitions.Clear();
对于(int i=0;i<(int)e.NewValue;i++)
添加(newRowDefinition(){Height=GridLength.Auto});
//设置箭头(网格);
}
#端区
#区域列计数属性
/// 
///将指定数量的列添加到ColumnDefinitions。默认宽度为“自动”
/// 
公共静态只读DependencyProperty ColumnCountProperty=
DependencyProperty.RegisterAttached(“ColumnCount”,类型为(int),
类型(网格属性),
新属性元数据(-1,ColumnCountChanged));
//得到
公共静态整型GetColumnCount(DependencyObject obj)
{
返回(int)对象GetValue(ColumnCountProperty);
}
//设置
公共静态void SetColumnCount(DependencyObject对象,int值)
{
对象设置值(ColumnCountProperty,value);
}
//更改事件-添加列
公共静态void ColumnCountChanged(DependencyObject对象、DependencyPropertyChangedEventArgs e)
{
如果(!(对象是网格)| |(int)e.NewValue<0)
返回;
网格网格=(网格)obj;
grid.ColumnDefinitions.Clear();
对于(int i=0;i<(int)e.NewValue;i++)
添加(新ColumnDefinition(){Width=GridLength.Auto});
//柱(网格);
}
#端区
}
结果


我尝试了这一点,但在检测到“容器生成”后试图清除RowDefinitions,我得到一个异常“无法在只读状态下修改“RowDefinitionCollection”。“这是一个全新的问题!”)对的我会再发一个。这里对我不起作用。当它是typeof(网格)时,我得到了一个例外。已更改为typeof(GridHelpers)并从dependencyobject派生-行/列计数更改的属性未被调用,即使源项目已更改。@Avi它似乎对我正常工作。我将更新我的答案,以包含我的实际工作代码,这可能与我在博客上发布的代码不同(自从我发布后,它被编辑了一些)@Avi此外,请确保您绑定到
int
,因为DependencyProperty设置为使用
int
。另一种数字类型,如
十进制
将不起作用
itemsControl1.ItemContainerGenerator.StatusChanged += ic_GeneratorStatusChanged;
void ic_GeneratorStatusChanged(object sender, EventArgs e)
{

    if (itemsControl1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
        return;

    itemsControl1.ItemContainerGenerator.StatusChanged -= ic_GeneratorStatusChanged;

    // your items are now generated
}
<Grid local:GridHelpers.RowCount="{Binding RowCount}"
      local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
<Grid ShowGridLines="True"
      local:GridProperties.ColumnCount="{Binding SomeInt}"
      local:GridProperties.RowCount="{Binding SomeInt}">

    <TextBox Text="Test" Grid.Row="0" Grid.Column="0" />
    <TextBox Text="Test" Grid.Row="0" Grid.Column="1" />
    <TextBox Text="Test" Grid.Row="0" Grid.Column="2" />
    <TextBox Text="Test" Grid.Row="1" Grid.Column="0" />
    <TextBox Text="Test" Grid.Row="1" Grid.Column="1" />
    <TextBox Text="Test" Grid.Row="1" Grid.Column="2" />
    <TextBox Text="Test" Grid.Row="2" Grid.Column="0" />
    <TextBox Text="Test" Grid.Row="2" Grid.Column="1" />
    <TextBox Text="Test" Grid.Row="2" Grid.Column="2" />
</Grid>
public class GridProperties
{
    #region RowCount Property

    /// <summary>
    /// Adds the specified number of Rows to RowDefinitions. Default Height is Auto
    /// </summary>
    public static readonly DependencyProperty RowCountProperty =
        DependencyProperty.RegisterAttached("RowCount", typeof(int),
        typeof(GridProperties),
        new PropertyMetadata(-1, RowCountChanged));

    // Get
    public static int GetRowCount(DependencyObject obj)
    {
        return (int)obj.GetValue(RowCountProperty);
    }

    // Set
    public static void SetRowCount(DependencyObject obj, int value)
    {
        obj.SetValue(RowCountProperty, value);
    }

    // Change Event - Adds the Rows
    public static void RowCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || (int)e.NewValue < 0)
            return;

        Grid grid = (Grid)obj;
        grid.RowDefinitions.Clear();

        for (int i = 0; i < (int)e.NewValue; i++)
            grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

        //SetStarRows(grid);
    }

    #endregion

    #region ColumnCount Property

    /// <summary>
    /// Adds the specified number of Columns to ColumnDefinitions. Default Width is Auto
    /// </summary>
    public static readonly DependencyProperty ColumnCountProperty =
        DependencyProperty.RegisterAttached("ColumnCount", typeof(int),
        typeof(GridProperties),
        new PropertyMetadata(-1, ColumnCountChanged));

    // Get
    public static int GetColumnCount(DependencyObject obj)
    {
        return (int)obj.GetValue(ColumnCountProperty);
    }

    // Set
    public static void SetColumnCount(DependencyObject obj, int value)
    {
        obj.SetValue(ColumnCountProperty, value);
    }

    // Change Event - Add the Columns
    public static void ColumnCountChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (!(obj is Grid) || (int)e.NewValue < 0)
            return;

        Grid grid = (Grid)obj;
        grid.ColumnDefinitions.Clear();

        for (int i = 0; i < (int)e.NewValue; i++)
            grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });

        // SetStarColumns(grid);
    }

    #endregion
}