2011-04-20 2 views
1

다음 코드에는 텍스트 상자와 이미지가 포함 된 ListBoxItem 스타일이 있습니다. 텍스트 블록에 ListBoxItem 내용에 대한 바인딩이 있고 이미지가 스타일에 설정되어 있습니다. 각 listboxitem 및 코드에서이 이미지를 설정할 수 있기를 원하면 사람이 올바른 길로 나를 안내 할 수 있는지 궁금합니다.스타일 랩핑에서 이미지의 소스를 할당하는 방법 ListBoxItem

도움 주셔서 감사합니다.

코드 스타일의 조각과 그것을 사용 :

<Style x:Key="ExpanderListitemStyle" TargetType="{x:Type ListBoxItem}"> 
<StackPanel Orientation="Horizontal"> 
    <Image x:Name="iGalleryPreview" Source="images/someImage.png" Width="18" Height="18"/> 
    <TextBlock x:Name="con" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Text="{TemplateBinding Content}" 
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="5,0,0,0"> 
    <TextBlock.Foreground> 
     <SolidColorBrush Color="#FF2A485C" x:Name="contentColor" /> 
     </TextBlock.Foreground> 
    </TextBlock> 
</StackPanel> 

<ListBox BorderThickness="0" ItemContainerStyle="{DynamicResource ExpanderListitemStyle}" VerticalAlignment="Stretch" Height="Auto"> 
    <ListBoxItem Content="Some Label" /> 
    <ListBoxItem Content="Another Label"/> 
</ListBox> 

답변

1

의 우리는 다음과 같이 우리는 각 직원에 대한 그녀의 이름과 이미지를 저장, Employee 클래스가 가정 해 봅시다 :

public class Employee 
{ 
    public string Image { get; set; } 
    public string Name { get; set; } 
} 

을 대신의 StyleItemTemplate을 다음과 같이 사용해야합니다.

<Window x:Class="ProofOfConcept.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ListBox ItemsSource="{Binding}"> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="ListBoxItem"> 
        <Style.Triggers> 
         <Trigger Property="IsSelected" Value="True"> 
          <Setter Property="TextBlock.Foreground" 
            Value="Green"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="TextBlock.Foreground" 
            Value="Red"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </ListBox.ItemContainerStyle> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" > 
         <Image Stretch="UniformToFill" Height="24" 
            Width="24" Margin="2,1" 
            Source="{Binding Image}"/> 
         <TextBlock Text="{Binding Name}" 
             VerticalAlignment="Center"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Window> 
using System.Windows; 

namespace ProofOfConcept 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      var emps = 
       new Employee[] 
        { 
         new Employee {Image = "/Images/1.png", Name = "Mohamed A. Fadil"}, 
         new Employee {Image = "/Images/6.png", Name = "Sara Konor :)"}, 
         new Employee {Image = "/Images/2.png", Name = "John M. Arther"}, 
         new Employee {Image = "/Images/3.png", Name = "Khalid El-Sheikh"}, 
         new Employee {Image = "/Images/4.png", Name = "Hassan A. Fadil"}, 
         new Employee {Image = "/Images/5.png", Name = "Criss Redfield"} 
        }; 
      DataContext = emps; 
     } 
    } 
} 

그리고 마지막 결과 : 14,뒤에 코드는 다음과 같이 될 것입니다 D

THe Final Result

+0

내 스타일은 ControlTemplate은 많은 색상을 통해 마우스, 선택한 색상, 애니메이션과 같은 색상을 변경하는 않습니다 항목 등 위에 마우스를 놓으면됩니다. itemtemplate을 사용하여이 작업을 계속 수행 할 수 있습니까? 죄송합니다. wpf에 꽤 새로 왔습니다. – Terco

+0

@ 모타이, 둘 다 설정할 수 있습니다. 업데이트 된 XAML 코드 –

+0

고마워요. 선택한 경우 어떻게 배경을 바꿀 것입니까? 포어 그라운드를 변경 한 예제에서 작업했는데, 배경으로 변경했다면 listboxitem의 기본 선택된 색상을 얻었습니다. 또한 listboxitem 배경을 투명하게 설정 한 다음 컨트롤 주위에 테두리를 감싸고 배경을 설정했습니다. 기본 선택한 색상은 여전히 ​​나타납니다. 어떤 아이디어를 주셔서 감사합니다 – Terco

관련 문제