2014-01-10 49 views
1

我有帶圓角圓角網格角匹配窗口圓角

<Window x:Name="windowPortal" x:Class="ICS2GO.PortalWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Portal" Height="301" Width="489" Icon="/Resources/icon.ico" 
    WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Closing="Window_Closing" Background="Transparent" AllowsTransparency="True"> 

<Border Name="windowBorder" BorderThickness="2" BorderBrush="DarkBlue" 
    CornerRadius="20" Background="LightBlue" Margin="0,0,0,0"> 

    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
     //main controls, buttons, images ...etc here 

     <Grid x:Name="gdWait" > 
      <Grid Background="Black" Opacity="0.5"/> 
      <Label x:Name="lblStatus" Content="Please Wait" 
       HorizontalAlignment="Center" HorizontalContentAlignment="Center" 
       VerticalContentAlignment="Center" VerticalAlignment="Center" 
       FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,51,28,62" 
       Height="72" Width="410"/> 

      <ProgressBar x:Name="pbWaiting" HorizontalAlignment="Left" Height="30" 
       Margin="110,108,0,0" VerticalAlignment="Top" Width="243" 
       IsIndeterminate="True" Orientation="Horizontal"/> 
     </Grid> 
    </Grid> 

網格X WPF窗口:名稱=「gbWait」被顯示在所有主控制與黑色背景和不透明度設置爲允許一些主要控件的可粘性,但總是讓它們不可點擊的用戶

我想使網格gbWait的圓角也是圓形的,所以它與窗口的圓角相匹配。目前他們是正方形的,並延伸到正常方形的窗戶角落。

回答

3

按如下所示使用邊框的Clip屬性來實現您的要求。

<Border Name="windowBorder" BorderThickness="2" BorderBrush="DarkBlue" 
     CornerRadius="20" Background="LightBlue" Margin="0,0,0,0"> 
     <Border.Clip> 
      <RectangleGeometry RadiusX="20" RadiusY="20" Rect="0,0,489,301" > 
      </RectangleGeometry> 
     </Border.Clip>  
     <Grid></Grid>  
    </Border> 

此解決方案假定您的窗口大小爲489 x 301並且不可調整大小。如果您需要可調整大小的窗口的解決方案,則使用轉換器來計算RectangleGeometry的Rect值。

+0

這正是我所需要的。非常感謝。 – Tsukasa

1

我認爲這可能是一個很好的候選轉換器。

將這段代碼在你的代碼隱藏,或者如果你想有一個單獨的文件:

public class VisibilityToBrushConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
     var visible = (Visibility)value; 

     return visible == Visibility.Visible 
      ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(70, 130, 180)) 
      : new SolidColorBrush(System.Windows.Media.Color.FromRgb(173, 216, 230)); 
    } 

    public object ConvertBack(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

然後在你的XAML引用它(刪除<Grid Background="Black" Opacity="0.5"/>):

<Window.Resources> 
    <l:VisibilityToBrushConverter x:Key="converter" /> 
</Window.Resources> 
<Grid> 
    <Border Name="windowBorder" BorderThickness="2" BorderBrush="SteelBlue" CornerRadius="20" 
      Background="{Binding ElementName=gdWait, Path=Visibility, Converter={StaticResource converter}}" Margin="0,0,0,0"> 
     <Grid x:Name="gdWait" Visibility="Visible"> 
      <Label x:Name="lblStatus" Content="Please Wait" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,51,28,62" Height="72" Width="410"/> 
      <ProgressBar x:Name="pbWaiting" HorizontalAlignment="Left" Height="30" Margin="110,108,0,0" VerticalAlignment="Top" Width="243" IsIndeterminate="True" Orientation="Horizontal"/> 
     </Grid> 
    </Border> 
</Grid> 
+0

我使用它的效果,所以它爲我的目的。 – Tsukasa

+0

@Tsukasa我明白你在做什麼。這是一個可以保持圓角效果的替代解決方案。相應地更改顏色。最上面的一個是當內部網格可見時,而最下面的一個是隱藏的。 –

+0

什麼是轉換器?資源轉換器無法解析 – Tsukasa

0

我建議您使用填充物製作圓形邊框,並使用與您的網格背景相同的背景

<Border CornerRadius="10" Padding="10" Background=Black> 
<Grid x:Name="gdWait" Visibility="Collapsed"> 
<Grid Background="Black" Opacity="0.5"/> 
    <Label x:Name="lblStatus" Content="Please Wait" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,51,28,62" Height="72" Width="410"/> 
    <ProgressBar x:Name="pbWaiting" HorizontalAlignment="Left" Height="30" Margin="110,108,0,0" VerticalAlignment="Top" Width="243" IsIndeterminate="True" Orientation="Horizontal"/> 
</Grid> 
</Border> 
+0

這確實可以讓它們保持平直,但在視覺上它看起來並不像我的圓角那麼好。最糟糕的是最糟糕的,這是我可能必須解決的,如果沒有解決方案 – Tsukasa

0

試試這個

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:hp="clr-namespace:WpfApplication2" 
    Title="Portal" Height="301" Width="489" Template="{DynamicResource WindowTemplate}" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Background="Transparent" AllowsTransparency="True"> 
<Window.Resources> 
    <ControlTemplate x:Key="WindowTemplate"> 
     <Border BorderBrush="DarkBlue" Background="LightBlue" BorderThickness="2" Margin="5" CornerRadius="20"> 
      <Border BorderBrush="DarkBlue" Background="#576C73" Margin="5" BorderThickness="2" CornerRadius="20"> 
       <Grid VerticalAlignment="Stretch" Background="#576C73" HorizontalAlignment="Stretch" Margin="5"> 
        <Grid x:Name="gdWait" Margin="5" Background="#576C73" > 
         <Grid Background="#576C73"/> 
         <Label x:Name="lblStatus" Content="Please Wait" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" FontSize="24" Margin="28,0,28,62" Height="72" Width="410"/> 
         <ProgressBar x:Name="pbWaiting" HorizontalAlignment="Left" Height="30" Margin="110,108,0,0" VerticalAlignment="Top" Width="243" IsIndeterminate="True" Orientation="Horizontal"/> 
        </Grid> 
       </Grid> 
      </Border> 
     </Border> 
    </ControlTemplate> 
</Window.Resources> 

我已經改變了窗口模板..和它工作在不同大小的窗口的..對不起,如果我錯了,您的要求。