2011-08-02 18 views
1

静的リソースとして定義された2つのデータテンプレートがあります。今私はそれらの名前のそれぞれとコンボボックスを持っています。コンボボックス内の選択された静的リソースの名前にバインドするために、これらの静的リソースをitemtemplatesとして利用するコントロールを持つことは可能ですか?Silverlight:スタティックリソース名をコンボボックスにバインド

ありがとうございます。

答えて

0

これは非常に狂気に見えますが、合理的にうまくいくものです。

<Grid x:Name="LayoutRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <ComboBox x:Name="cboTemplate" DisplayMemberPath="Key" SelectedValuePath="Key" SelectedValue="Blue"> 
     <ComboBox.ItemsSource> 
      <ResourceDictionary> 
       <DataTemplate x:Key="Blue"> 
        <TextBlock Foreground="Blue" Text="{Binding}" /> 
       </DataTemplate> 
       <DataTemplate x:Key="Red"> 
        <TextBlock Foreground="Red" Text="{Binding}" /> 
       </DataTemplate> 
      </ResourceDictionary> 
     </ComboBox.ItemsSource> 
    </ComboBox> 
    <ContentPresenter Grid.Row="1" Content="Hello World" ContentTemplate="{Binding SelectedItem.Value, ElementName=cboTemplate}" /> 
</Grid> 

リソースディクショナリXAMLの解析がx:Key属性を使用して、それを移入して、脚を使い切る与え除き、fundementally KeyValuePair<object, object>のコレクションです。したがって、このComboBoxはプロパティを表示しますが、SelectedItemはとなります。そのValueプロパティはDataTemplateです。 ContentTemplateまたはItemTemplateプロパティで要素から要素へのバインディングを使用できるようになりました。

静的リソースとしてDataTemplatesを使用する必要があり、コードの重複を避けたい場合は、別のリソースディクショナリxamlファイルに配置できます。その後、使用することができます: -

 <ComboBox.ItemsSource> 
      <ResourceDictionary Source="DataTemplateResources.xaml" /> 
     </ComboBox.ItemsSource> 

のと同様のような静的リソースにそれらを含める: -

<UserControl.Resources> 
    <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="DataTemplateResources.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
      <!-- other resources here --> 
    </ResourceDictionary> 
</UserControl.Resources> 
+0

ない、非常に私は私が考えるの後に探していたもの。私は、この "pushpinTemplate"をコンボボックス内の現在選択されている項目の値にバインドしたいと思っています(ItemTemplate = "{StaticResource PushpinTemplate}" />) 。 – mortenbpost

+0

@Mortenbpost:ComboBoxはリソースディクショナリをアイテムソースとして使用できません。 – AnthonyWJones

関連する問題