C# 如何将类字段绑定到自定义模板中的元素?(WPF)
如何将类字段绑定到自定义ListBoxItem模板内的元素?? 这个示例代码缺少一些方面,如类定义、列表框名称等,我只是在处理绑定过程C# 如何将类字段绑定到自定义模板中的元素?(WPF),c#,wpf,binding,C#,Wpf,Binding,如何将类字段绑定到自定义ListBoxItem模板内的元素?? 这个示例代码缺少一些方面,如类定义、列表框名称等,我只是在处理绑定过程 <ListBox> <ListBox.Resources> <Style TargetType="ListBoxItem"> <Setter Property="ContentTemplate"> <Setter.Value>
<ListBox>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<!-- how to bind class fields to these elements -->
<StackPanel Orientation="Horizontal">
<Label></Label>
<Image></Image>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<!-- adding items dynamically -->
</ListBox>
我明白了。C级:
C#动态添加:
ListBoxItem lbi = new ListBoxItem();
lbi.Content = new Hello() { Name = "hello man", Description="I am superman.", ImagePath="Images/myimage.png"};
menu.Items.Add(lbi);
XAML:
在WPF中开发时,鼓励MvvM促进关注点的分离。要做到这一点,需要实现一个视图模型,其属性随后将绑定到视图。当您希望UI(视图)了解视图模型提供的数据更改时,必须实现
INotifyPropertyChanged
界面,如下所示:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
namespace ViewModels
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public BaseViewModel()
{
//ctor
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
UIThread( () =>
{
//make sure the event is raised on the main thread
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
});
}
Dispatcher _dispacther;
protected void UIThread(Action action)
{
if (_dispacther == null)
{
_dispacther = Dispatcher.CurrentDispatcher;
}
_dispacther.Invoke(action);
}
}
}
您的Hello
类将从BaseViewModel派生,并提供OnPropertyChanged();在这样的财产中:
private string name;
public string Name
{
get { return name; }
set { name= value; OnPropertyChanged(); }
}
现在,当您更改列表框中所选项目的名称时,它将反映在UI中。试试看!
此实现确保在主线程上引发事件,因此在调用事件时不需要在中执行此操作。另外,
[CallerMemberName]
将填充您调用的属性的名称 请记住,必须实现INotfyPropertyChanged接口,否则您的更改将不会反映在屏幕上!绑定只支持属性。请声明所引用的更改。
<ListBox Name="menu" ItemsSource="{Binding}">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label FontWeight="Bold" Content="{Binding Name}"></Label>
<Label Content="{Binding Description}"></Label>
<Image Source="{Binding ImagePath}" Width="30" Height="30"></Image>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
namespace ViewModels
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public BaseViewModel()
{
//ctor
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
UIThread( () =>
{
//make sure the event is raised on the main thread
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
});
}
Dispatcher _dispacther;
protected void UIThread(Action action)
{
if (_dispacther == null)
{
_dispacther = Dispatcher.CurrentDispatcher;
}
_dispacther.Invoke(action);
}
}
}
private string name;
public string Name
{
get { return name; }
set { name= value; OnPropertyChanged(); }
}