2013-06-01 85 views
1

是否可以從UserControl本身訪問已定義的資源?怎麼樣?從自己訪問UserControl的資源

我試圖做到這一點:

<UserControl xmlns="all defined namespaces here..." 
      Visibility="{Binding Show,FallbackValue=Hidden, 
       Converter={StaticResource BooleanToVisibility}}"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <BooleanToVisibilityConverter x:Key="BooleanToVisibility" /> 
     </ResourceDictionary> 
    </UserControl.Resources> 
</UserControl> 

,但我得到這個錯誤:

'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '11' and line position '9'.

回答

3

或許可以改變這個順序:

<UserControl.Resources> 
    <ResourceDictionary> 
     <BooleanToVisibilityConverter x:Key="BooleanToVisibility" /> 
    </ResourceDictionary> 
</UserControl.Resources> 
<UserControl.Visibility> 
    <Binding Path="Show" FallbackValue="Hidden" 
      Converter="{StaticResource BooleanToVisibility}"/> 
</UserControl.Visibility> 

你也可以移動轉換器到Application.Resources,因爲它通常在多個地方使用,那麼它可以在任何地方被引用。

+0

我是多麼醉心:D非常感謝。完美的想法 –