2012-01-05 20 views
9

मेरे पास एक बहुत ही सरल WPF एप्लिकेशन है जो कॉम्बोबॉक्स प्रदर्शित करता है जो लोगों का प्रतिनिधित्व करने वाले वर्गों की एक सूची से जुड़ता है। प्रत्येक 'व्यक्ति' ऑब्जेक्ट में एक नाम स्ट्रिंग फ़ील्ड होता है, और एक सेक्स एनम होता है। मैं कॉम्बोबॉक्स को विभिन्न लोगों के नाम फ़ील्ड के ड्रॉप-डाउन को प्रदर्शित करना चाहता हूं, लेकिन सेक्स लाइन के अनुसार प्रत्येक पंक्ति को स्टाइल करने के लिए, उदाहरण के लिए, पुरुषों के लिए नीला, महिलाओं के लिए गुलाबी। कोई मुझे बता सकता हैं मुझसे क्या गलती हो रही है? यहाँस्टाइलिंग डब्ल्यूपीएफ कॉम्बोबॉक्स आइटम

<Window x:Class="ComboBoxColour.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"> 
    <StackPanel Orientation="Vertical"> 
     <ComboBox ItemsSource="{Binding People}" Width="100" Height="20"> 
      <ComboBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Name="somePerson" Text="{Binding Path=Name}">       
         <TextBlock.Triggers> 
          <DataTrigger Binding="{Binding Path=Sex}" Value="Male"> 
           <DataTrigger.Setters> 
            <Setter Property="Foreground" Value="Blue" TargetName="somePerson" /> 
           </DataTrigger.Setters> 
          </DataTrigger> 
         </TextBlock.Triggers>       
        </TextBlock> 
       </DataTemplate> 
      </ComboBox.ItemTemplate> 
     </ComboBox> 
    </StackPanel> 
</Window> 

और सी # है:

यहाँ एक्सएमएल है

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 

namespace ComboBoxColour 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
    public List<Person> people; 
    public List<Person> People 
    { 
     get { return people; } 
     set { people = value; } 
    } 

    public MainWindow() 
    { 
     this.DataContext = this; 

     People = new List<Person>(); 
     People.Add(new Person("Alice", SexEnum.Female)); 
     People.Add(new Person("Bob", SexEnum.Male)); 
     People.Add(new Person("Claire", SexEnum.Female)); 
     People.Add(new Person("Daniel", SexEnum.Male)); 

     InitializeComponent(); 
    } 
    } 

    public enum SexEnum{Male,Female}; 

    public class Person 
    { 
    private string name; 
    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    private SexEnum sex; 
    public SexEnum Sex 
    { 
     get { return sex; } 
     set { sex = value; } 
    } 

    public Person(string Name, SexEnum Sex) 
    { 
     this.Name = Name; 
     this.Sex = Sex; 
    } 
    } 
} 

अग्रिम में बहुत धन्यवाद

उत्तर

15

आप का उपयोग करना चाहिए "शैली" "TextBlock.Triggers के बजाय चलाता है "

इस एक्सएएमएल का उपयोग करें:

<Window x:Class="ComboBoxColour.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"> 
    <StackPanel Orientation="Vertical"> 
     <ComboBox ItemsSource="{Binding People}" Width="100" Height="20"> 
      <ComboBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Name="somePerson" Text="{Binding Path=Name}"> 
         <TextBlock.Style> 
          <Style TargetType="TextBlock"> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding Path=Sex}" Value="Male"> 
             <DataTrigger.Setters> 
              <Setter Property="Foreground" Value="blue"/> 
             </DataTrigger.Setters> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Sex}" Value="Female"> 
             <DataTrigger.Setters> 
              <Setter Property="Foreground" Value="Pink"/> 
             </DataTrigger.Setters> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </TextBlock.Style> 
        </TextBlock> 
       </DataTemplate> 
      </ComboBox.ItemTemplate> 
     </ComboBox> 
    </StackPanel> 
</Window> 

अब आपके पास पुरुष के लिए नर और गुलाबी के लिए गुलाबी होगा।

18

उपयोग ItemContainerStyleItemTemplate के बजाय:

<ComboBox ItemsSource="{Binding People}" Width="100" Height="20"> 
     <ComboBox.ItemContainerStyle> 
      <Style TargetType="ComboBoxItem"> 
       <Setter Property="Foreground" Value="Pink" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=Sex}" Value="Male"> 
         <Setter Property="Foreground" Value="Blue" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ComboBox.ItemContainerStyle> 
    </ComboBox> 
संबंधित मुद्दे