2011-01-07 4 views
0

例として、template.xamlに以下のコードがあります。xamlからC#にリソースディクショナリを使用して要素を取得する方法はありますか?

<Border x:Name="PART_ButtonNormal" Grid.Column="0"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Border Name="PART_ImageBorder" Grid.Column="0"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 
       <Image Margin="2" Width="16"           Source="{Binding Path=SmallIcon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/> 
       <Border Height="20" Grid.Row="1" Background="Red"/> 
      </Grid> 
     </Border> 
     <TextBlock Grid.Column="1" x:Name="PART_Text" Text="{TemplateBinding Label}" 
            Foreground="{TemplateBinding Foreground}" 
            FontFamily="{TemplateBinding FontFamily}" 
            FontSize="{TemplateBinding FontSize}" 
            VerticalAlignment="{TemplateBinding VerticalAlignment}"       
            HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
            Margin="2,0,4,0"/> 
    </Grid> 
</Border> 

このxamlは、リソースディクショナリを使用してWrapper.csクラスで読み取られました。次に、Wrapper.csのtemplate.xamlからImage要素にアクセスするにはどうすればいいですか?

私に解決策を教えてください。テンプレートは、実際のControlに適用された後

よろしく、 デビッド・C

答えて

0

あなたがImageインスタンスをしたいですか? Controlに適用する前にControlTemplate自体を変更しようとしている場合は、可能かどうかはわかりません。

public Image FindImage(Control parent) 
{ 
    Queue<DependencyObject> items = new Queue<DependencyObject>(); 
    items.Enqueue(parent); 

    while (items.Count > 0) 
    { 
     var item = items.Dequeue() as Visual; 
     if (item is Image) 
      return item; 

     var count = VisualTreeHelper.GetChildrenCount(item); 
     for (int i = 0; i < count; ++i) 
      items.Enqueue(VisualTreeHelper.GetChild(item, i)); 
    } 
} 
:テンプレートが適用されている与えられた Controlからの画像を取得しようとしている場合

しかし、あなただけのビジュアルツリーを歩くことができます

関連する問題