usercontrol中的wpf数据绑定不起作用

编程入门 行业动态 更新时间:2024-10-27 17:20:42
本文介绍了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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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.cs

public 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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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.cs

public 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数据绑定不起作用

本文发布于:2023-11-10 06:01:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   不起作用   数据   usercontrol   wpf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!