在 XAML 中绑定到 Window.Current.Bounds.Width



我在LayoutAware页面上有一个弹出控件。

我真正想要的是弹出窗口填满屏幕。

我认为解决方案是使用 Window.Current.Bounds.Height/Width 在弹出控件内的网格上设置相应的属性。

我不想使用代码隐藏文件来设置这些属性。我希望能够绑定到 XAML 中的 Window.Current.Bounds.Height。

我可以这样做吗?

有没有更好的方法让弹出窗口填满屏幕?

您可以通过编写高度和宽度转换器来做到这一点。

public class WidthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return Window.Current.Bounds.Width;
    }
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
public class HeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return Window.Current.Bounds.Height;
    }
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

将其添加到您的页面资源部分 -

    <common:WidthConverter x:Key="wc" />
    <common:HeightConverter x:Key="hc" />

将它们用于您的弹出窗口 -

        <Popup x:Name="myPopup"  >
            <Grid  Background="#FFE5E5E5" Height="{Binding Converter={StaticResource hc}}" Width="{Binding Converter={StaticResource wc}}" />
        </Popup>

您可以使用转换器(请参阅打字员)或者使用静态类。

在应用中:

<datamodel:Foo x:Name="FooClass" />
xmlns:datamodel="using:MyProject.Foo.DataModel"

在你的 xaml 中:

Source="{Binding Source={StaticResource FooClass}, Path=Width}"

其中 Width 是类中的一个属性,返回 Window.Current.Bounds.Width。

样品 : public double Width{get{return Window.Current.Bounds.Width;}}

问候。

相关内容

最新更新