初始网格行高不起作用 [英] Init grid row height doesn't work
问题描述
这是我简单的try-it应用程序,它创建了一个2行的网格。第一行的高度绑定到一个属性。我分配给它的值只在运行时起作用。我试图让它在设计时也可以工作,但我没有这样做(我使用这个线程来写我的应用程序)。
请帮我看看我想念的。谢谢!
我想动态设置顶部网格行的高度,即。 Grid.Row =0
,作为标题栏高度。在我的应用程序的某个地方,视图加载并重叠标题栏。 你正在尝试做一个非常奇怪的诡计,这不应该工作。尝试进行以下更改。
MainWindow.xaml.cs - 尽量始终让代码隐藏起来。
namespace WpfTryIt
{
///< summary>
/// 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>
还有一个新的独立数据上下文类,根据模式的不同有所不同:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.ComponentModel;
使用System.Windows;
namespace WpfTryIt
{
public class FakeDataContext
{
public int BindingHeight
{
get
{
//检查设计模式。
if((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject))。DefaultValue))
{
//设计模式
return 100;
}
else
{
return 200;
}
}
}
}
}
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.
解决方案 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;
}
}
}
}
}
这篇关于初始网格行高不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!