2011-09-09 56 views
1

我有一個相當奇怪的問題,不想調整大小的控件。 Bellow是用SL4編寫的控制背後的XAML +代碼。用戶控件不調整大小

因此,爲了解釋一些事情,控件有4個區域 - Header,Subheader,BodyContent和Footer。每個區域都由一個「Border」元素表示,該元素充當其嵌套子元素的容器。

這個控制只是爲了統一我的應用程序的外觀和感覺,並沒有真正做任何事情就邏輯。

我的問題是,如果我想使用該控件,我必須明確地設置它的寬度。使用元素到元素綁定(對父容器)設置寬度時,控件甚至不會呈現。

我還嘗試創建一個「公共新的雙寬/高」屬性,其中我將設置控件本身的寬度和「ControlBorder」元素的寬度。使用這個屬性還會使控件在運行時不渲染:(我也嘗試過連接Size Changed事件,但它一直給我發出循環錯誤!

關於如何使這個控件更「可調整大小」的任何想法?應用程序是在採用網格分配器網格使用它。這是絕對必要的控制知道如何調整自己!

任何幫助將不勝感激! 感謝

馬丁

<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="SL4.Learning.LayoutPanel" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <UserControl.Resources> 
     <SolidColorBrush x:Key="DarkBrush" Color="#7B4C4C4B"/> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Border x:Name="ControlBorder" Grid.Row="0" Grid.Column="0" BorderBrush="{StaticResource DarkBrush}" BorderThickness="2" CornerRadius="4" Margin="2"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition x:Name="LayoutHeaderRow" Height="30"/> 
        <RowDefinition x:Name="LayoutSubHeaderRow" Height="30"/> 
        <RowDefinition x:Name="LayoutContentRow" Height="*"/> 
        <RowDefinition x:Name="LayoutFooterRow" Height="30"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <Rectangle x:Name="PnlHeaderBackground" Grid.Row="0" Grid.Column="0" Fill="{StaticResource DarkBrush}"/> 
       <toolkit:DockPanel Grid.Row="0" Grid.Column="0" LastChildFill="False"> 
        <TextBlock x:Name="LblHeaderTitle" Text="Header Text" FontSize="12" Foreground="White" toolkit:DockPanel.Dock="Left" VerticalAlignment="Center" Margin="4,0,0,0"/> 
        <Border x:Name="HeaderBorder" toolkit:DockPanel.Dock="Right" BorderThickness="0" BorderBrush="Transparent" Margin="0" CornerRadius="0"> 

        </Border> 
       </toolkit:DockPanel> 
       <Border x:Name="SubHeaderBorder" Grid.Row="1" Grid.Column="0" BorderThickness="0" BorderBrush="Transparent" Margin="0" CornerRadius="0"> 

       </Border> 
       <Border x:Name="ContentBorder" Grid.Row="2" Grid.Column="0" BorderThickness="0" BorderBrush="Transparent" Margin="0" CornerRadius="0"> 

       </Border> 
       <Border x:Name="FooterBorder" Grid.Row="3" Grid.Column="0" BorderThickness="0" BorderBrush="Transparent" Margin="0" CornerRadius="0"> 

       </Border> 
      </Grid> 
     </Border> 
    </Grid> 
</UserControl> 

而後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Data; 

namespace SL4.Learning 
{ 
    public partial class LayoutPanel : UserControl 
    { 
     public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register("BorderRadius", typeof(CornerRadius), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderTextProperty = DependencyProperty.Register("HeaderText", typeof(string), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderBackgroundProperty = DependencyProperty.Register("HeaderBackground", typeof(Brush), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderForegroundProperty = DependencyProperty.Register("HeaderForeground", typeof(Brush), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderFontSizeProperty = DependencyProperty.Register("HeaderFontSize", typeof(double), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderFontFamilyProperty = DependencyProperty.Register("HeaderFontFamily", typeof(FontFamily), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderFontWeightProperty = DependencyProperty.Register("HeaderFontWeight", typeof(FontWeight), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderFontStyleProperty = DependencyProperty.Register("HeaderFontStyle", typeof(FontStyle), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderFontStretchProperty = DependencyProperty.Register("HeaderFontStretch", typeof(FontStretch), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty HeaderContentProperty = DependencyProperty.Register("HeaderContent", typeof(UIElement), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty SubHeaderContentProperty = DependencyProperty.Register("SubHeaderContent", typeof(UIElement), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty BodyContentProperty = DependencyProperty.Register("BodyContent", typeof(UIElement), typeof(KeystoneContentControl), null); 
     public static readonly DependencyProperty FooterContentProperty = DependencyProperty.Register("FooterContent", typeof(UIElement), typeof(KeystoneContentControl), null); 

     public KeystoneContentControl() 
     { 
      InitializeComponent(); 

     } 

     public new double Width 
     { 
      get { return this.Width; } 
      set 
      { 
       this.Width = value; 
       this.ControlBorder.Width = value; 
      } 
     } 

     public new double Height 
     { 
      get { return this.Height; } 
      set 
      { 
       this.Height = value;     
       this.ControlBorder.Height = value; 
      } 
     } 

     public new Brush BorderBrush 
     { 
      get { return this.ControlBorder.BorderBrush; } 
      set { this.ControlBorder.BorderBrush = value; } 
     } 

     public new Thickness BorderThickness 
     { 
      get { return this.ControlBorder.BorderThickness; } 
      set { this.ControlBorder.BorderThickness = value; } 
     } 

     public CornerRadius BorderRadius 
     { 
      get{ return (CornerRadius)this.GetValue(KeystoneContentControl.BorderRadiusProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.BorderRadiusProperty, value); 
       this.ControlBorder.CornerRadius = value; 
      } 
     } 

     public Brush HeaderBackground 
     { 
      get{ return (Brush)this.GetValue(KeystoneContentControl.HeaderBackgroundProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderBackgroundProperty, value); 
       this.PnlHeaderBackground.Fill = value; 
      } 
     } 

     public Brush HeaderForeground 
     { 
      get{ return (Brush)this.GetValue(KeystoneContentControl.HeaderForegroundProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderForegroundProperty, value); 
       this.LblHeaderTitle.Foreground = value; 
      } 
     } 

     public double HeaderFontSize 
     { 
      get { return (double)this.GetValue(KeystoneContentControl.HeaderFontSizeProperty); } 
      set 
      { 
       this.LblHeaderTitle.FontSize = value; 
      } 
     } 

     public FontFamily HeaderFontFamily 
     { 
      get { return (FontFamily)this.GetValue(KeystoneContentControl.HeaderFontFamilyProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderFontFamilyProperty, value); 
       this.LblHeaderTitle.FontFamily = value; 
      } 
     } 

     public FontWeight HeaderFontWeight 
     { 
      get{ return (FontWeight)this.GetValue(KeystoneContentControl.HeaderFontWeightProperty); } 
      set 
      { 
       this.SetValue(HeaderFontWeightProperty, value); 
       this.LblHeaderTitle.FontWeight = value; 
      } 
     } 

     public FontStyle HeaderFontStyle 
     { 
      get { return (FontStyle)this.GetValue(KeystoneContentControl.HeaderFontStyleProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderFontStyleProperty, value); 
       this.LblHeaderTitle.FontStyle = value; 
      } 
     } 

     public FontStretch HeaderFontStretch 
     { 
      get { return (FontStretch)this.GetValue(KeystoneContentControl.HeaderFontStretchProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderFontStretchProperty, value); 
       this.LblHeaderTitle.FontStretch = value; 
      } 
     } 

     public string HeaderText 
     { 
      get { return (string)this.GetValue(KeystoneContentControl.HeaderTextProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderTextProperty, value); 
       this.LblHeaderTitle.Text = value; 
      } 
     } 

     public UIElement HeaderContent 
     { 
      get{ return (UIElement)this.GetValue(KeystoneContentControl.HeaderContentProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.HeaderContentProperty, value); 
       this.HeaderBorder.Child = value; 
      } 
     } 

     public UIElement SubHeaderContent 
     { 
      get{ return (UIElement)this.GetValue(KeystoneContentControl.SubHeaderContentProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.SubHeaderContentProperty, value); 
       this.SubHeaderBorder.Child = value; 
      } 
     } 

     public UIElement BodyContent 
     { 
      get { return (UIElement)this.GetValue(KeystoneContentControl.BodyContentProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.BodyContentProperty, value); 
       this.ContentBorder.Child = value; 
      } 
     } 

     public UIElement FooterContent 
     { 
      get{ return (UIElement)this.GetValue(KeystoneContentControl.FooterContentProperty); } 
      set 
      { 
       this.SetValue(KeystoneContentControl.FooterContentProperty, value); 
       this.FooterBorder.Child = value; 
      } 
     } 
    } 
} 

編輯:婁是該用戶控件的頁面

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SL4.Learning.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="600" 
    d:DesignWidth="800" 
    xmlns:my="clr-namespace:SL4.Learning"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition x:Name="LeftCol" /> 
      <ColumnDefinition x:Name="RightCol" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition x:Name="RowHeight" /> 
     </Grid.RowDefinitions> 
     <my:LayoutPanel Grid.Row="0" Grid.Column="0" Width="{Binding ElementName=LeftCol, Path=ActualWidth}" HorizontalAlignment="Left" x:Name="PnlLayout" VerticalAlignment="Top"> 
      <my:LayoutPanel.SubHeaderContent> 
       <TextBlock x:Name="SubHeaderLabel" Text="SubHeader" FontWeight="Bold" VerticalAlignment="Center"/> 
      </my:LayoutPanel.SubHeaderContent> 
      <my:LayoutPanel.BodyContent> 
       <TextBlock x:Name="LblContentText" Text="BodyContent" VerticalAlignment="Center"/> 
      </my:LayoutPanel.BodyContent> 
      <my:LayoutPanel.FooterContent> 
       <TextBlock x:Name="LblFooterText" Text="Footer" VerticalAlignment="Center"/> 
      </my:LayoutPanel.FooterContent> 
     </my:LayoutPanel> 
    </Grid> 
</UserControl> 
+0

你能舉一個你使用用戶控件的例子嗎?用戶控件的大小設置爲* not *,默認情況下它將需要伸展(因爲您的外部網格行/ col大小設置爲「star」)。 –

+0

@HiTech Magic我剛剛添加了控件的用法。你的意思是「用戶控件的大小沒有設置爲伸展?」這是控制網格的一個選項嗎? – bleepzter

+1

這是一個選項*你插入你的控件*。將Horizo​​ntalAlignment和VerticalAlignment設置爲在外頁面中的SL4.Learning.LayoutPanel對象的實例上拉伸。 –

回答

0

用戶控件的大小沒有設置默認情況下,它需要伸展的用途(因爲您的外部網格行/列大小設置爲「星號」)。

這是一個選項,您可以插入您的控件。將Horizo​​ntalAlignment和VerticalAlignment設置爲在您的外部頁面上的SL4.Learning.LayoutPanel對象的實例上拉伸。

相關問題