根据内部的绑定值将不同的图像添加到组合框

时间:2019-02-17 17:13:34

标签: c# wpf xaml combobox

我对WPF和c#还是很陌生。我用组合框编码了一个简单的表单,并向该组合框添加了一张图片,该图片将位于文本旁边。图像将根据人物类别的性别而变化。

我只想知道这是做这样事情的正确做法,还是我应该采取更好的方法?

我的代码也可以按我的意愿工作,但是我很沮丧,我正在学习坏习惯。

我使用了一个单独的属性来确定图片的URL,这似乎不是解决此类问题的最佳方法。

任何指针都将非常有帮助。

谢谢

    public partial class MainWindow : Window
    {
        private List<Person> people = new List<Person>();

        public MainWindow()
        {
            InitializeComponent();
            people.Add(new Person { Firstname = "Sinead", Lastname = "McCoy" , Sex = "Female" });
            people.Add(new Person { Firstname = "Andrew", Lastname = "Warner", Sex = "Other" });
            people.Add(new Person { Firstname = "Ailish", Lastname = "Man" , Sex = "Female" });
            people.Add(new Person { Firstname = "Andrew", Lastname = "McCory", Sex = "Other" });
            people.Add(new Person { Firstname = "Andrew", Lastname = "McCall" , Sex = "Male" });


            //people.Add(a);
            cmbMyComboBox.ItemsSource = people;

        }

下面是我的Person类:

class Person
{
    private string _sex;
    private string _cmbimage;
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Sex
    {
        get
        {
            var sex = new string[] { "Male", "Female" };

            if (sex.Contains(_sex))
            {
               return _sex;
            }
            else
                _sex = "Other";
                return _sex;
        }

        set
        {
                _sex = value;
        }
    }

    public string Fullname
    {
        get
        {
            return $"{Firstname} {Lastname}";
        }

    } 

    public string Cmbimage
    {
        get
        {
            if (_sex == "Male")
            {
                _cmbimage = "C:\\Users\\richa\\Pictures\\Icons\\WPFdemo\\cmbimage.png";
                return _cmbimage;
            }
            if (_sex == "Female")
            {
                _cmbimage = "C:\\Users\\richa\\Pictures\\Icons\\WPFdemo\\cmbimagegirl.png";
                return _cmbimage;
            }
            else
                _cmbimage = "C:\\Users\\richa\\Pictures\\Icons\\WPFdemo\\cmbimageother.png";
                return _cmbimage;
        }
    }

}

这是我用于组合框的Xaml:

    <ComboBox x:Name="cmbMyComboBox" Grid.Column="1"
              Grid.Row="3" Grid.ColumnSpan="2" Margin="10">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Cmbimage}" Height="20" Width="20"/>
                    <TextBlock Text="{Binding Fullname}"/>
                    <!--<TextBlock Text=" "/>
                    <TextBlock Text="{Binding Lastname}"/>
                    <TextBlock Text=" "/>
                    <TextBlock Text="{Binding Sex}"/>-->
                </StackPanel>                
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

编辑:

   class Person
    {
        private Uri _cmbimage;


    public Uri Cmbimage
    {
        get
        {
            if (_sex == "Male")
            {
                _cmbimage = new Uri("pack://application:,,,/Images/cmb_male.png");
                return _cmbimage;
            }
            if (_sex == "Female")
            {
                _cmbimage = new Uri("pack://application:,,,/Images/cmb_female.png");
                return _cmbimage;
            }
            else
                _cmbimage = new Uri("pack://application:,,,/Images/cmb_other.png");
                return _cmbimage;
        }
    }

}

数据触发器编辑XAML:

 <Window.Resources>
        <BitmapImage x:Key="Image1" UriSource="pack://application:,,,/Images/cmb_Male.png"/>
        <BitmapImage x:Key="Image2" UriSource="pack://application:,,,/Images/cmb_Female.png"/>
        <BitmapImage x:Key="Image3" UriSource="pack://application:,,,/Images/cmb_Other.png"/>
    </Window.Resources>


    <ComboBox x:Name="cmbMyComboBox" Grid.Column="1"
              Grid.Row="3" Grid.ColumnSpan="2" Margin="10">
        <ComboBox.ItemTemplate>

            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="image" Height="20" Width="20"/>
                    <TextBlock Text="{Binding Fullname}"/>
                </StackPanel>

                <DataTemplate.Triggers>

                    <DataTrigger Binding="{Binding Sex}" Value="Male">
                        <Setter TargetName="image" Property="Source"
                             Value="{StaticResource Image1}"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding Sex}" Value="Female">
                        <Setter TargetName="image" Property="Source"
                            Value="{StaticResource Image2}"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding Sex}" Value="Other">
                        <Setter TargetName="image" Property="Source"
                             Value="{StaticResource Image3}"/>
                    </DataTrigger>

                </DataTemplate.Triggers>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

0 个答案:

没有答案