初始化网格行高不起作用

发布于 2024-09-28 11:03:53 字数 415 浏览 0 评论 0原文

这是我的简单尝试应用程序,它创建一个包含 2 行的网格。第一行的高度绑定到一个属性。我分配给它的值仅在运行时有效。我试图让它在设计时也能工作,但我没能做到这一点(我使用了

请帮助我看看我错过了什么。谢谢你!

[编辑]

我这样做的原因是我想动态设置顶部网格行的高度,即。 Grid.Row="0",标题栏高度。在我的应用程序中的某个位置,视图已加载并与标题栏重叠。

this is my simple try-it application that create a grid with 2 rows. The 1st row's height is bound to a properties. The value I assigned to it only works at run-time. I tried to make it also work when design-time but I failed to do that (I used this thread to write my app).

Please help me to see what I miss. Thank you!

[Edit]

The reason why I do this is that I want to set dynamically the height of the top grid row, ie. Grid.Row="0", to be the title bar height. Somewhere in my app, the view loaded and overlap the title bar.

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

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

发布评论

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

评论(1

习ぎ惯性依靠 2024-10-05 11:03:53

你正在尝试做一个非常奇怪的把戏,这是不应该起作用的。尝试进行以下更改。

MainWindow.xaml.cs - 尽量保持代码隐藏清晰。

namespace WpfTryIt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfTryIt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"

        xmlns:s ="clr-namespace:WpfTryIt"
        >
    <Window.DataContext>
        <s:FakeDataContext></s:FakeDataContext>
    </Window.DataContext>


        <Button Content="{Binding Path=BindingHeight}"/>

</Window>

以及一个新的单独数据上下文类,其行为因模式而异:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace WpfTryIt
{
    public class FakeDataContext
    {
        public int BindingHeight
        {
            get
            {
                // Check for design mode. 
                if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
                {
                    //in Design mode
                    return 100;
                }
                else
                {
                    return 200;
                }
            }
        }
    }
}

You're trying to do a very strange trick, which is not supposed to work. Try to make the following changes.

MainWindow.xaml.cs -- try to always keep you code-behind clear.

namespace WpfTryIt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfTryIt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"

        xmlns:s ="clr-namespace:WpfTryIt"
        >
    <Window.DataContext>
        <s:FakeDataContext></s:FakeDataContext>
    </Window.DataContext>


        <Button Content="{Binding Path=BindingHeight}"/>

</Window>

And a new separate data context class, which behave different depending on the mode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace WpfTryIt
{
    public class FakeDataContext
    {
        public int BindingHeight
        {
            get
            {
                // Check for design mode. 
                if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
                {
                    //in Design mode
                    return 100;
                }
                else
                {
                    return 200;
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文