2013-07-10 143 views
2

使用視框中我有ResourceFile1.xaml文件 其內容在資源字典文件

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<Viewbox x:Key="Flash-On" > 
    <Grid Width="256" Height="256" Visibility="Visible"> 
     <Path Tag="Icon" Data="F1M376.251,632.755L385.665,632.755 381.302,646.07 394.618,646.07 389.11,660.302 393.01,660.302 381.531,672.93 377.398,660.763 381.073,660.763 383.829,652.268 369.825,652.268 376.251,632.755z" Stretch="Uniform" Fill="#FFFFFFFF" Width="176" Height="176" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5"> 
      <Path.RenderTransform> 
       <TransformGroup> 
        <TransformGroup.Children> 
         <RotateTransform Angle="0" /> 
         <ScaleTransform ScaleX="1" ScaleY="1" /> 
        </TransformGroup.Children> 
       </TransformGroup> 
      </Path.RenderTransform> 
     </Path> 
    </Grid> 
</Viewbox> 


<Viewbox x:Key="Flash-Off"> 
      .... 
</Viewbox> 
</ResourceDictionary> 

代碼爲PhoneAppplicationPage

<phone:PhoneApplicationPage.Resources> 

    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ResourceFile1.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</phone:PhoneApplicationPage.Resources> 

但這種代碼不工作。 設計器錯誤:InvalidOperationException:元素已經是另一個元素的子元素。 也<Button Content="{StaticResource Flash-On}"/>如果我使用類似的代碼 運行時錯誤:未能分配給屬性'System.Windows.Controls.ContentControl.Content'。 如果viewbox在Grid.Resources中使用沒有問題,但我想使用ResourceDictionary.how可以嗎?

回答

5

因爲Silverlight resource dictionary中的所有內容都必須是可共享的。在WPF中,您可以在資源字典內的對象上使用x:Shared屬性來強制WPF爲每個資源檢索創建一個新實例。 爲了避免這種情況在Silverlight中你可以創建一個DataTemplate

<DataTemplate x:Key="ButtonTemplate"> 
    <Viewbox> 
<!-- Here your content--> 
    </Viewbox> 
</DataTemplate> 

<Button ContentTemplate="{StaticResource ButtonTemplate}"/> 

UPDATE 0

我寫的,其改變依賴於CheckBox的值模板的例子。

轉換器來更改模板:

public class TemplateSelectorConverter : IValueConverter 
{ 
    public DataTemplate TrueTemplate { get; set; } 

    public DataTemplate FalseTemplate { get; set; } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (((bool) value)) 
      return TrueTemplate; 
     return FalseTemplate; 
    } 

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

資源:

<DataTemplate x:Key="FirstTemplate"> 
    <TextBox Text="FirstTemplate" /> 
</DataTemplate> 

<DataTemplate x:Key="SecondTemplate"> 
    <TextBox Text="SecondTemplate" /> 
</DataTemplate> 

<internal:TemplateSelectorConverter x:Key="TemplateSelector" TrueTemplate="{StaticResource FirstTemplate}" 
            FalseTemplate="{StaticResource SecondTemplate}" /> 

XAML標記:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="30" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

    <CheckBox Name="CheckBox"/> 
    <Button Grid.Column="1" 
      ContentTemplate="{Binding Path=IsChecked, ElementName=CheckBox, Converter={StaticResource TemplateSelector}}" 
      VerticalAlignment="Top" /> 
</Grid> 

我希望這有助於。

+0

感謝工作正常,但不能使用轉換器class.how與轉換器類一起使用? – Oguzhan

+0

您是否嘗試過:'

+0

not working.not binding Path = ?? – Oguzhan