2012-11-20 62 views
4

我在LayoutAware頁面上有一個彈出控件。綁定到XAML中的Window.Current.Bounds.Width

我真正想要的是讓彈出窗口填滿屏幕。

我認爲解決方案是使用Window.Current.Bounds.Height/Width在彈出控件的網格內設置相應的屬性。

我不想使用文件後面的代碼來設置這些屬性。我希望能夠綁定到XAML中的Window.Current.Bounds.Height。

我可以這樣做嗎?

有沒有更好的方法來讓彈出窗口填滿屏幕?

回答

5

你可以通過編寫高度和寬度的轉換器來完成。

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> 
+0

我最終使用這個解決方案。 @Typist謝謝! – Robert

+1

這似乎沒有響應頁面大小的變化 - 停靠在左邊或右邊,或事件2/3大小。 – Nathan

4

您可以使用轉換器(見打字員) 或使用靜態類。

在你的App.xaml:

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

而在你的XAML:

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

其中寬度是在你的類屬性,它返回Window.Current.Bounds.Width。

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

問候。

+0

這也是一個很好的解決方案。如果我能看到自己使用5或6個屬性,我會創建一個靜態類。 @大衛謝謝! – Robert

+0

@Robert Super。如果它更好,爲什麼不用這個解決方案改變? – David

+0

我現在正在使用我的項目中的轉換器,如上所示。這就是爲什麼我將備選答案標記爲答案的原因。你和打字員的答案都適用於我的情況。我在我的應用程序中使用MVVM模式。第三種解決方案是將其作爲View Model的屬性。我不想爲每個View Model複製它。 – Robert