我一直在谷歌上搜索这个,但没有得到这个问题:
简单xaml:
<Window x:Class="WpfHeightBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfHeightBinding="clr-namespace:WpfHeightBinding"
Title="MainWindow"
Width="{Binding Width}"
Height="{Binding Height}"
SizeToContent="Manual"
>
<Window.DataContext>
<wpfHeightBinding:TheDataContext />
</Window.DataContext>
<Grid>
</Grid>
</Window>
我将"宽度"one_answers"高度"都绑定到数据上下文。这是cs代码
using System.Windows;
namespace WpfHeightBinding
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class TheDataContext
{
public int Width
{
get { return 200; }
}
public int Height
{
get { return 400; }
}
}
}
我为两个getter都设置了断点。仅提取第一个,忽略Height
。如果我在xaml中切换Height
和Width
,则只获取Height
,而忽略Width
。
我真的无法解释这种行为。窗户的高度似乎是任意的。我没有MinHeight
,没有其他影响,绑定会发生什么?没有错误。CCD_ 7无作用。
如果不使用它们,我会理解,因为它们可能是在DataContext
初始化之前提取的,但会查询DataContext
。
您需要在代码背后提供"Width"one_answers"Height"属性设置器,然后使绑定为"TwoWay":
XAML
<Window x:Class="WpfHeightBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfHeightBinding="clr-namespace:WpfHeightBinding"
Title="MainWindow"
Width="{Binding Width, Mode=TwoWay}"
Height="{Binding Height, Mode=TwoWay}"
SizeToContent="Manual"
>
<Window.DataContext>
<wpfHeightBinding:TheDataContext />
</Window.DataContext>
代码背后:
public class TheDataContext
{
public int Width { get { return 800; } set { } }
public int Height { get { return 200; } set { } }
}