本文介绍了usercontrol中的wpf数据绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 当我在窗口中运行以下代码时绑定才能正常工作,但是当我做同样的事情但是让它成为用户控制它停止工作时。 我尝试了很多东西,但我对WPF很新,需要帮助。 (现在代码在usercontrol和window之间有所不同,因为我正在尝试我所知道的每一件事;-) Window.xaml显示所需的行为。 请帮帮我:-) window.xaml Hello,when I run following code in a window the binding just works normally, But when I do the same thing but make it a usercontrol it stops working.I''ve tried so many things but I''m very very new to WPF and in need of help. (now the code differs between the usercontrol and the window because i''m trying every possible thing I know ;-) The Window.xaml displays the desired behaviour.Please help me :-)window.xaml<Window x:Class="testwpfgrid.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" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <StackPanel> <ListView Name="lv" ItemsSource="{Binding Path=AvosEntries}" IsSynchronizedWithCurrentItem="True"> <ListView.View> <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Avos Information"> <GridViewColumn DisplayMemberBinding="{Binding Path=AvosName}" Header="Avos Name" Width="100"/> </GridView> </ListView.View> </ListView> <Button HorizontalAlignment="Right" Margin="5,5,5,5" Content="Add Row" Click="AddRow_Click" /> </StackPanel></Window> window.xaml.cs window.xaml.cspublic partial class MainWindow : Window { public class AvosEntry { public string AvosName { get; set; } } public MainWindow() { AvosEntries = new ObservableCollection<AvosEntry>(); fillList(); InitializeComponent(); } private void AddRow_Click(object sender, RoutedEventArgs e) { AvosEntries.Add(new AvosEntry { AvosName = "New Avos Entry" }); } private void fillList() { for (int i = 1; i <= 10; i++) { AvosEntry avosEntry = new AvosEntry(); avosEntry.AvosName = "Avos " + i; AvosEntries.Add(avosEntry); } } public ObservableCollection<AvosEntry> AvosEntries { get; private set; } } usercontrol.xaml usercontrol.xaml<UserControl x:Class="Genesyslab.Desktop.Modules.Extension.avos.MySample.MySampleView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <ListView Name="lv" ItemsSource="{Binding Path=AvosEntries}" Foreground="#FFEF1212" removed="#FF868686" IsSynchronizedWithCurrentItem="True"> <ListView.View> <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Avos Information"> <GridViewColumn DisplayMemberBinding="{Binding Path=AvosName}" Header="Avos Name" Width="100"/> </GridView> </ListView.View> </ListView> <Button HorizontalAlignment="Right" Margin="5,5,5,5" Content="Add Row" Click="AddRow_Click" /> </StackPanel></UserControl> usercontrol.xaml.cs usercontrol.xaml.cspublic partial class MySampleView : UserControl{ public MySampleView() { AvosEntries = new ObservableCollection<AvosEntry>(); fillList(); InitializeComponent(); Width = Double.NaN; Height = Double.NaN; lv.DataContext = AvosEntries; } private void AddRow_Click(object sender, RoutedEventArgs e) { AvosEntries.Add(new AvosEntry { AvosName = "New Avos Entry" }); } private void fillList() { for (int i = 1; i <= 10; i++) { AvosEntry avosEntry = new AvosEntry(); avosEntry.AvosName = "Avos " + i; AvosEntries.Add(avosEntry); } } public ObservableCollection<AvosEntry> AvosEntries { get { return (ObservableCollection<AvosEntry>)GetValue(AvosEntriesProperty); } set { SetValue(AvosEntriesProperty, value); } } public static DependencyProperty AvosEntriesProperty = DependencyProperty.Register("AvosEntries", typeof(ObservableCollection<AvosEntry>), typeof(MySampleView), new PropertyMetadata(new ObservableCollection<AvosEntry>())); 推荐答案 我在你的代码中看到的问题是你试图绑定ItemsSource以使用你的AvosEntries DataContext(恰好是AvosEntries属性指向的集合)。基本上,您尝试使用AvosEntries集合的AvosEntries属性。 我看到两个简单的修复: *将DataContext更改为您的MySampleView实例。其中包含AvosEntries属性。 *或者,将ItemsSource中的绑定更改为{Binding}。这将告诉它使用DataContext中的项目。 作为旁注,您可能希望将AvosEntries属性的默认值更改为null(除非您是尝试制作某种伪单例集合。The issue I see in you code is that you are trying to bind the ItemsSource to use the AvosEntries of your DataContext (which happens to be the collection pointed to by your AvosEntries property). Basically, you are trying to use the AvosEntries property of the AvosEntries collection. I see two easy fixes: * Change the DataContext to your instance of MySampleView. Which has an AvosEntries property. * Or, change the binding in the ItemsSource to just "{Binding}". This will tell it to use the item in the DataContext.As a side note, you might want to change the default value of AvosEntries property to null (unless you are trying to make some kind of pseudo-singleton collection). 尝试命名UserControl并在Path之前的绑定中使用ElementName try to name the UserControl and use ElementName within the binding right before the Path 这篇关于usercontrol中的wpf数据绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 02:04