WPF 控件初始化时属性未更新

发布于 2024-11-10 07:19:44 字数 1697 浏览 2 评论 0原文

我是 WPF 新手,在从 MainWindow XAML 文件获取自定义用户控件的属性值时遇到问题。

在这里,我想获取值“8”作为行数和列数,但在我的 InitializeGrid() 方法中,从未设置这些属性。它们始终为“0”。我做错了什么?

任何参考文献也将不胜感激。


这是我的 MainWindow.xaml(相关部分):

<local:BoardView 
    BoardRows="8" 
    BoardColumns="8"
    />

这是我的 BoardView.xaml:

<UniformGrid 
        Name="uniformGrid" 
        Rows="{Binding BoardRows}"
        Columns="{Binding BoardColumns}"
        >

    </UniformGrid>
</UserControl>

这是我的 BoardView.xaml.cs:

[Description("The number of rows for the board."),
 Category("Common Properties")]
public int BoardRows
{
    get { return (int)base.GetValue(BoardRowsProperty); }
    set { base.SetValue(BoardRowsProperty, value); }
}
public static readonly DependencyProperty BoardRowsProperty =
    DependencyProperty.Register("BoardRows", typeof(int), typeof(UniformGrid));

[Description("The number of columns for the board."),
 Category("Common Properties")]
public int BoardColumns
{
    get { return (int)base.GetValue(BoardColumnsProperty); }
    set { base.SetValue(BoardColumnsProperty, value); }
}
public static readonly DependencyProperty BoardColumnsProperty =
    DependencyProperty.Register("BoardColumns", typeof(int), typeof(UniformGrid));

public BoardView()
{
    InitializeComponent();
    DataContext = this;
    InitializeGrid();
}

private void InitializeGrid()
{
    int rows = BoardRows;
    int cols = BoardColumns;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            uniformGrid.Children.Add( ... );
            // ...
        }
    }
}

I'm new to WPF, and am having trouble getting the values for properties for a custom user control from the MainWindow XAML file.

Here, I want to get the value "8" as the number of rows and columns but in my InitializeGrid() method, the properties are never set. They are always "0". What am I doing wrong?

Any references will also be appreciated.


This is my MainWindow.xaml (the relevant portions):

<local:BoardView 
    BoardRows="8" 
    BoardColumns="8"
    />

This is my BoardView.xaml:

<UniformGrid 
        Name="uniformGrid" 
        Rows="{Binding BoardRows}"
        Columns="{Binding BoardColumns}"
        >

    </UniformGrid>
</UserControl>

And this is my BoardView.xaml.cs:

[Description("The number of rows for the board."),
 Category("Common Properties")]
public int BoardRows
{
    get { return (int)base.GetValue(BoardRowsProperty); }
    set { base.SetValue(BoardRowsProperty, value); }
}
public static readonly DependencyProperty BoardRowsProperty =
    DependencyProperty.Register("BoardRows", typeof(int), typeof(UniformGrid));

[Description("The number of columns for the board."),
 Category("Common Properties")]
public int BoardColumns
{
    get { return (int)base.GetValue(BoardColumnsProperty); }
    set { base.SetValue(BoardColumnsProperty, value); }
}
public static readonly DependencyProperty BoardColumnsProperty =
    DependencyProperty.Register("BoardColumns", typeof(int), typeof(UniformGrid));

public BoardView()
{
    InitializeComponent();
    DataContext = this;
    InitializeGrid();
}

private void InitializeGrid()
{
    int rows = BoardRows;
    int cols = BoardColumns;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            uniformGrid.Children.Add( ... );
            // ...
        }
    }
}

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

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

发布评论

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

评论(1

-黛色若梦 2024-11-17 07:19:44

您已设置此绑定:

<UserControl ...>
    <UniformGrid 
        Name="uniformGrid" 
        Rows="{Binding BoardRows}"
        Columns="{Binding BoardColumns}"
        >

    </UniformGrid>
</UserControl>

问题是您的绑定不起作用,因为该绑定使用默认数据源,即 UserControlDataContext。您可能还没有设置DataContext,但这没关系,因为无论如何这都不是您想要的。

您想要将 UniformGrid 中的 Rows 数量绑定到 BoardView.BoardRows 属性。由于UserControl是前面的代码片段一个BoardView,您可以给BoardView一个名称并使用ElementName 语法如下引用它:

<UserControl Name="boardView" ...>
    <UniformGrid 
        Name="uniformGrid" 
        Rows="{Binding BoardRows, ElementName=boardView}"
        Columns="{Binding BoardColumns, ElementName=boardView}"
        >

    </UniformGrid>
</UserControl>

这表示:“将 UniformGrid.Row 绑定到名为 boardView 的元素的 BoardRows 属性”,就是你的 想!

You have this binding set up:

<UserControl ...>
    <UniformGrid 
        Name="uniformGrid" 
        Rows="{Binding BoardRows}"
        Columns="{Binding BoardColumns}"
        >

    </UniformGrid>
</UserControl>

The problem is that your binding is not working because the binding uses the default data source which is the DataContext of the UserControl. You probably haven't set the DataContext but that's OK because that's not what you want anyway.

You want to bind the number of Rows in the UniformGrid to the BoardView.BoardRows property. Since the UserControl is the previous code snippet is a BoardView, you can give the BoardView a name and use the ElementName syntax to refer to it like this:

<UserControl Name="boardView" ...>
    <UniformGrid 
        Name="uniformGrid" 
        Rows="{Binding BoardRows, ElementName=boardView}"
        Columns="{Binding BoardColumns, ElementName=boardView}"
        >

    </UniformGrid>
</UserControl>

This says: "Bind UniformGrid.Row to the BoardRows property of the element named boardView", just what you want!

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