2012-04-11 149 views
1

我已經創建了一個基於擴展類的定製控件的自定義WPF控件:如何允許寬度=「自動」,在

public partial class HideableExpander : Expander 
{ 
    public new double Height 
    { 
     get 
     { 
      if (Visibility== System.Windows.Visibility.Hidden) 
      { 
       return 0; 
      } 
      return base.Height; 
     } 
     set 
     { 
      base.Height = value; 
     } 
    } 

    public new double Width 
    { 
     get 
     { 
      if (Visibility == System.Windows.Visibility.Hidden) 
      { 
       return 0; 
      } 
      return base.Width; 
     } 
     set 
     { 
      base.Width = value; 
     } 
    } 

    public new Thickness Margin 
    { 
     get 
     { 
      if (Visibility== System.Windows.Visibility.Hidden) 
      { 
       return new Thickness(); 
      } 
      return base.Margin; 
     } 
     set 
     { 
      base.Margin = value; 
     } 
    } 

    public HideableExpander() 
    { 

     this.InitializeComponent(); 

    } 
} 

XAML:

<Expander 
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" 
x:Class="leartWPF.HideableExpander" 
x:Name="UserControl" 
d:DesignWidth="640" d:DesignHeight="480"> 

<Grid x:Name="LayoutRoot"/> 
</Expander> 

現在,當我嘗試與WIDTH =「自動」屬性中使用它:

  <local:HideableExpander Header="{Binding Expander1Name, ElementName=Window}" Margin="10" Width="Auto" Background="#00F19494" VerticalAlignment="Top" > 
       <WrapPanel Height="Auto" Margin="0" Width="Auto" > 
         <TextBlock Text="Please, enter the name of this expander:  " VerticalAlignment="Center"/> 
         <TextBox Width="150" Text="{Binding Expander1Name, ElementName=Window, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Background="#FFF5EECC"/> 
       </WrapPanel> 
      </local:HideableExpander> 

我得到有關無法轉換「自動」雙例外:

Unhandled Exception: System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '10' and line position '8'. ---> System.Exception: Auto is not a valid value for Double. ---> System.FormatException: Input string was not in a correct format. 
    at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) 
    at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider) 
    at System.ComponentModel.DoubleConverter.FromString(String value, NumberFormatInfo formatInfo) 
    at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) 
    --- End of inner exception stack trace --- 
    at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) 
    at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider) 
    --- End of inner exception stack trace --- 
    at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 
    at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) 
    at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) 
    at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc) 
    at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) 
    at System.Windows.Application.DoStartup() 
    at System.Windows.Application.<.ctor>b__1(Object unused) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
    at System.Threading.ExecutionContext.runTryCode(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.ProcessQueue() 
    at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    at System.Windows.Application.RunInternal(Window window) 
    at System.Windows.Application.Run() 
    at leartWPF.App.Main() in d:\Users\menkaur\Documents\Expression\Blend 4\Projects\leartWPF\leartWPF\obj\Debug\App.g.cs:line 0 

我應該怎麼做才能夠接受Auto作爲一個值?

+1

那麼設置Visibility = Collapsed是什麼問題? – Phil 2012-04-11 17:49:30

+0

酷!謝謝。實際上更好 – 2012-04-11 18:04:06

回答

4

嘗試增加以下屬性,我認爲應該幫助,當然,「新」隱藏父母的寬度......

[TypeConverterAttribute(typeof(LengthConverter))] 
public new double Width 
{ 
    ......your getter and setter 

// 編輯 每個評論:「什麼是吸氣劑和二流體?

//getter and setter sample(grabbing them from the question) 
    get 
    { 
     if (Visibility == System.Windows.Visibility.Hidden) 
     { 
      return 0; 
     } 
     return base.Width; 
    } 
    set 
    { 
     base.Width = value; 
    } 
} 
+1

對不起,對於我們這些新來WPF的人來說,你能舉一個例子來說明getter和setter的內容嗎?他們如何使用這個? – 2013-12-06 15:51:52

+0

get/set是訪問者的暴露財產,認爲他們讀/寫到你的班級中的一些私人領域。原因 - 封裝一個私有變量,狀態,控制。你不希望你的類的用戶直接訪問它,也許你可以在它們上添加一些邏輯,比如對get進行解析,初始化或格式化,或者對set進行相同的標記驗證或事件。在這個問題中,情況正是如此,他們沒有直接暴露base.Width,但是如果Window被隱藏,則邏輯寬度被返回爲零。希望它是有道理的... – 2013-12-07 00:43:34

+0

我理解getters和setter的概念。我要求提供一個完整的例子,說明他們內部的代碼特別適合這種情況。注意我的問題不是「什麼是吸氣劑和二流體?」。這是「你能舉一個例子,說明吸氣和吸氣器的內容。」你提供的是哪個! :) 謝謝! – 2014-01-02 16:51:08

相關問題