Wpf 使用Dependency属性绑定到UserControl中ListBox的ItemsSource
我面临着一个奇怪的问题。这似乎是一个众所周知的问题。我试图找到解决办法。花了整整一天的时间。但还是没有用。我尝试过的所有解决方案对我都没有帮助Wpf 使用Dependency属性绑定到UserControl中ListBox的ItemsSource,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,我面临着一个奇怪的问题。这似乎是一个众所周知的问题。我试图找到解决办法。花了整整一天的时间。但还是没有用。我尝试过的所有解决方案对我都没有帮助 <ListBox ItemsSource="{Binding ElementName=root,Path=ItemsSource,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Category" Background="Coral"></ListBox>
<ListBox ItemsSource="{Binding ElementName=root,Path=ItemsSource,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Category" Background="Coral"></ListBox>
现在,在MainWindow.xaml中,
我创建了一个用户控件的实例,并通过这种方式绑定到Usercontrol的ItemsSource
ItemsSource="{Binding ElementName=Window,Path=LineBarData,UpdateSourceTrigger=PropertyChanged}"
其中windows是主窗口的名称
主窗口后面的代码是:
public partial class MainWindow : Window
{
private ObservableCollection<LineBarChartData> lineBarData;
internal ObservableCollection<LineBarChartData> LineBarData
{
get
{
return lineBarData;
}
set
{
lineBarData = value;
}
}
public MainWindow()
{
InitializeComponent();
LineBarData = new ObservableCollection<LineBarChartData>();
LineBarData.Add(new LineBarChartData() { Category = "January", ValueX = 10, ValueY = 50 });
LineBarData.Add(new LineBarChartData() { Category = "February", ValueX = 20, ValueY = 60 });
LineBarData.Add(new LineBarChartData() { Category = "March", ValueX = 30, ValueY = 70 });
LineBarData.Add(new LineBarChartData() { Category = "April", ValueX = 40, ValueY = 80 });
}
列表框中未显示任何内容。我找不到我的错误。请帮忙
谢谢。WPF数据绑定仅适用于公共属性,因此
internal ObservableCollection<LineBarChartData> LineBarData { get; set; }
请注意,在绑定上设置
UpdateSourceTrigger=PropertyChanged
仅在双向或单向ToSource绑定时有效,并且该绑定实际上更新了源属性。在绑定中不是这样的 这是有道理的,我按照你的建议修改了代码。但我仍然没有看到任何结果(运行应用程序时,是否在Visual Studio的输出窗口中看到任何绑定错误消息?请确保两个绑定中的ElementName实际引用了有效的元素,即在UserControl的XAML中设置了x:Name=“root”
,并且x:Name=“Window”
在主窗口的XAML中。我在输出窗口中没有看到任何绑定错误,而且元素名称是正确的:)您能再试一次吗?它现在工作得很好。。我犯了一个小错误。。我忘了取下。。(LineBarData=新的ObservableCollection();)
public class LineBarChartData : INotifyPropertyChanged
{
private string _category;
public string Category
{
get { return this._category; }
set { this._category = value; this.OnPropertyChanged("Category"); }
}
private double _valueX;
public double ValueX
{
get { return this._valueX; }
set { this._valueX = value; this.OnPropertyChanged("ValueX"); }
}
private double _valueY;
public double ValueY
{
get { return this._valueY; }
set { this._valueY= value; this.OnPropertyChanged("ValueY"); }
}
//private Brush _color;
//public Brush Color
//{
// get { return this._color; }
// set { this._color = value; this.OnPropertyChanged("Color"); }
//}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
internal ObservableCollection<LineBarChartData> LineBarData { get; set; }
public ObservableCollection<LineBarChartData> LineBarData { get; set; }
public ObservableCollection<LineBarChartData> LineBarData { get; }
= new ObservableCollection<LineBarChartData>();
public MainWindow()
{
InitializeComponent();
LineBarData.Add(new LineBarChartData() { ... });
...
}