Create ItemTemplate for ListBox in code-behind in WPF - Part II

╄→尐↘猪︶ㄣ 提交于 2019-12-23 22:34:38

问题


I named this question of mine Part II since I've already asked a similar but simpler question about creating an ItemTemplat for ListBox in WPF in here Create ItemTemplate for ListBox in code-beind in WPF

Now I'm going to expand my question. I want to have an ItemTemplate for a ListBox so that it can be used either with or without binding to an ObservableCollection.

If I don't want to bind the ItemsSource to an ObservableCollection I use the code as follows:

var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
textBlockFactory.SetValue(TextBlock.TextProperty, new Binding(".")); // Here
textBlockFactory.SetValue(TextBlock.BackgroundProperty, Brushes.Red);
textBlockFactory.SetValue(TextBlock.ForegroundProperty, Brushes.Wheat);
textBlockFactory.SetValue(TextBlock.FontSizeProperty, 18.0);

var template = new DataTemplate();            
template.VisualTree = textBlockFactory;

MyListBox.ItemTemplate = template;

But it doesn't work for ItemsSource binding to an ObservableCollection since the TextBlock.TextProperty must binds to the DisplayMemberPath property.

Sorry for bad grammar.


回答1:


First of all you need to create a variable that will determine the state: are using a collection, or just an array of strings. This flag can also be a dependency property, in my example it's a SomeFlag:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    bool SomeFlag = false;

    if (SomeFlag == false)
    {
        var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
        textBlockFactory.SetValue(TextBlock.TextProperty, new Binding("."));

        textBlockFactory.SetValue(TextBlock.BackgroundProperty, Brushes.Red);
        textBlockFactory.SetValue(TextBlock.ForegroundProperty, Brushes.Wheat);
        textBlockFactory.SetValue(TextBlock.FontSizeProperty, 18.0);

        var template = new DataTemplate();
        template.VisualTree = textBlockFactory;

        MyListBox.ItemTemplate = template;
    }
    else
    {
        MyListBox.DisplayMemberPath = "Name";
        MyListBox.SelectedValuePath = "Age";
    }
}

And for testing, add this handler of SelectionChanged event:

private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("SelectedValue is " + MyListBox.SelectedValue);
}

Below is a full example:

XAML

<Grid>
    <ListBox Name="MyListBox"
             SelectionChanged="MyListBox_SelectionChanged"
             ItemsSource="{Binding Path=MyCollection}" />
</Grid>

Code-behind

public partial class MainWindow : Window
{
    ViewModel MyViewModel = new ViewModel();

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = MyViewModel;

        MyViewModel.MyCollection = new ObservableCollection<Person>();

        MyViewModel.MyCollection.Add(new Person()
        {
            Age = 22,
            Name = "Nick",
        });

        MyViewModel.MyCollection.Add(new Person()
        {
            Age = 11,
            Name = "Sam",
        });

        MyViewModel.MyCollection.Add(new Person()
        {
            Name = "Kate",
            Age = 15,
        });
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        bool SomeFlag = false;

        if (SomeFlag == false)
        {
            var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
            textBlockFactory.SetValue(TextBlock.TextProperty, new Binding("."));

            textBlockFactory.SetValue(TextBlock.BackgroundProperty, Brushes.Red);
            textBlockFactory.SetValue(TextBlock.ForegroundProperty, Brushes.Wheat);
            textBlockFactory.SetValue(TextBlock.FontSizeProperty, 18.0);

            var template = new DataTemplate();
            template.VisualTree = textBlockFactory;

            MyListBox.ItemTemplate = template;
        }
        else
        {
            MyListBox.DisplayMemberPath = "Name";
            MyListBox.SelectedValuePath = "Age";
        }
    }

    private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("SelectedValue is " + MyListBox.SelectedValue);
    }
}

public class ViewModel : NotificationObject
{
    #region MyCollection

    public ObservableCollection<Person> MyCollection
    {
        get;
        set;
    }

    #endregion
}

#region Model

public class Person
{
    public string Name
    {
        get;
        set;
    }

    public int Age
    {
        get;
        set;
    }
}

#endregion

#region NotificationObject

public class NotificationObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

#endregion


来源:https://stackoverflow.com/questions/23081311/create-itemtemplate-for-listbox-in-code-behind-in-wpf-part-ii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!