2012-04-03 3 views
1

シナリオ:Silverlightのアクセスリソース

私は、リストボックスから派生したカスタムコントロールを持っているいくつかのDataTemplateリソース

<DataTemplate x:Key="myDragCueTemplate"> 
     <Border Background="Blue" 
       Opacity="0.5" 
       Width="250"> 
      <TextBlock Text="{Binding}" HorizontalAlignment="Left"></TextBlock> 
     </Border> 
    </DataTemplate> 

を持つビューを持っています。特定のイベントのカスタムリストボックスの中で、私はViewのリソースからデータテンプレートを取得したいと思います。

public class MyListBox : ListBox 
{ 
    public MyListBox() 
    { 
     this.DefaultStyleKey = typeof(MyListBox); 
    } 
... 

itemDragCue.ContentTemplate = this.Resources["myDragCueTemplate"] as DataTemplate; 

... 

私は、別の.xamlファイルにdatatemplatesを追加しようとしたのResourceDictionaryを追加しましたが、それはまだそれを拾うませんでした。

カスタムコントロールのバックエンドでリソースを取得するにはどうすればよいですか?

ありがとうございました。

答えて

2

this.Resourcesは唯一私がのResourceDictionaryでmyDragCueTemplateを置くことをお勧めし

<UserControl x:Class="MyListbox"> 
    <UserControl.Resources> 

で宣言されたリソースを提供します。その場合、コード内でそのResourceDictionaryを読んで、必要な特定のリソースを抽出する必要があります。

チャームcadrell0のように、この

const string resourcesPath = "/AssemblyName;component/Resources.xaml"; 
Uri resourceUri = new Uri(resourcesPath, UriKind.Relative); 
StreamResourceInfo sri = Application.GetResourceStream(resourceUri); 
StreamReader sr = new StreamReader(sri.Stream); 
ResourceDictionary dictionary = (ResourceDictionary) XamlReader.Load(sr.ReadToEnd()); 
itemDragCue.ContentTemplate = dictionary["myDragCueTemplate"] as DataTemplate; 
+0

作品をお試しください。どうもありがとう! – asuciu

関連する問題