如何绑定窗口最大和最大从孩子开始大小

时间:2014-01-24 12:29:30

标签: c# wpf xaml data-binding window

这是我的Test Xaml,它不能像预期的那样工作

<Window x:Class="MyWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        WindowStartupLocation="CenterScreen"
        SizeToContent="Manual"
        Top="150"
        MinHeight="90" MinWidth="90"
             MaxHeight="{Binding ElementName=CPresenter, Path=MaxHeight,Mode=OneTime}"
             MaxWidth="{Binding ElementName=CPresenter, Path=MaxHeight,Mode=OneTime}"
             Height="{Binding ElementName=CPresenter, Path=Height,Mode=OneTime}"
             Width="{Binding ElementName=CPresenter, Path=MaxWidth,Mode=OneTime}">
    <Viewbox Name="viewer" 
             Stretch="Uniform">


        <ContentPresenter Name="CPresenter">
            <ContentPresenter.Content>
                <UserControl MaxWidth="337" MaxHeight="156">
                    <Grid Width="50" Height="50" Background="Red"
                      MaxHeight="50" MaxWidth="100"/>
                </UserControl>
            </ContentPresenter.Content>
        </ContentPresenter>

    </Viewbox>
</Window>

上面的代码应该设置Window MaxWidth&amp; Width337它还应设置MaxHeight&amp; Height156,但此绑定接缝不起作用。

没有错误它只是不使用绑定。

1 个答案:

答案 0 :(得分:0)

Binding来自名为MaxHeight的{​​{1}}元素的属性ContentPresenter,但您没有在该元素上设置这些属性。你将它们设置在里面的CPresenter。此外,您根本不需要UserControl元素。试试这个:

ContentPresenter

显然,您需要将<Viewbox Name="viewer" Stretch="Uniform"> <UserControl Name="CPresenter" Width="50" Height="50" MaxWidth="337" MaxHeight="156"> <Grid Background="Red" /> </UserControl> </Viewbox> 的名称更改为更好的名称,但这应该有效。