data-binding 相关问题

将两个数据/信息源绑定在一起并使它们保持同步的一般技术。

在代码隐藏中绑定到 DataTable 时,DataGrid 会部分更新

我有一个 WPF 窗口,其中有一个 DataGrid 作为其子窗口之一。 DataGrid 中的列数仅在运行时确定。以下是我的 window.xaml 文件中 DataGrid 的代码: <

回答 1 投票 0

如果文本框发生更改,如何获取事件?

我通过外部 yaml 文件进行了复杂的模型设计,并将视图添加为后面的代码。需要这个结构,因为我正在构建一种配置管理器,用户可以在其中创建视图布局......

回答 1 投票 0

如何在WPF MVVM中显示带有动态列的表格

我有一个对象列表,每个对象都包含用户定义的属性。这些属性的键和值是自由字符串,因此它们在对象之间不一定一致。无论如何,我

回答 1 投票 0

如何将 XSD 转换为 Python 类

我只是想知道是否有一个程序可以将 XSD 文件转换为 Python 类,就像 JAXB 对 Java 所做的那样?

回答 5 投票 0

在 WPF 中绑定数据值

我正在通过数据绑定来绑定文本框的数据。而不是像这样给出最小值和最大值: 我正在通过数据绑定绑定文本框的数据。而不是像这样给出最小值和最大值: <TextBox Height="24" HorizontalAlignment="Right" abc:TextBoxMaskBehaviour.Mask="Decimal" abc:TextBoxMaskBehaviour.MinimumValue="0" abc:TextBoxMaskBehaviour.MaximumValue="200" Margin="0,9,8.5,0" Name="txtCStart" VerticalAlignment="Top" Width="106" MouseWheel="OnMouseWheel"> 我想通过 .xaml.cs 文件给出它。如何做到这一点? 显然(从你的评论来看,你的问题不是很清楚),你想通过C#代码设置abc:TextBoxMaskBehaviour.MinimumValue和MaximumValue: TextBoxMaskBehaviour.SetMinimumValue(txtCStart, 0); TextBoxMaskBehaviour.SetMaximumValue(txtCStart, 200); 一般来说,您可以像这样设置附加属性:AttachedPropertyClass.SetAttachedProperty(Control, Value)。同样,可以使用 AttachedPropertyClass.GetAttachedProperty(Control). 读取该值。 这看起来可能与 Rubenhak TextBoxMask 功能直接相关(因此我发现它想要回答这个确切的问题)。对于任何关注鲁本哈克的人来说,答案是: Rubenhak.Common.WPF.TextBoxMaskBehavior.SetMask( TextBoxControl, Rubenhak.Common.WPF.MaskType.Decimal); 同样适用于: Rubenhak.Common.WPF.TextBoxMaskBehavior.SetMinimumValue() Rubenhak.Common.WPF.TextBoxMaskBehavior.SetMaximumValue()

回答 2 投票 0

OnLongClickListener 和数据绑定 - void 无法转换为布尔值(Android、Java)

如何将长按监听器与数据绑定集成? 它需要一个布尔值返回。 但显然我的方法无法返回值(void) 公共视图 onCreateView(@NonNull LayoutInflater inflat...

回答 1 投票 0

绑定到窗口高度和宽度的问题

当我尝试将窗口的高度和宽度绑定到视图模型中的属性时遇到一些问题。这是一个小示例应用程序来说明该问题。这是app.xaml.xs中的代码 公开

回答 11 投票 0

既然我已经完成了一些数据绑定到 mainpage.xaml

我对 .net maui 和 c# 总体来说是个新手,我正在尝试编写一个单词游戏,我遇到的问题是在我的主页上使用数据绑定时出现了一些问题: 主要...

回答 1 投票 0

OnPropertyChanged 不会触发仅 getter 属性的 getter

我的目标是触发领域对象(Group)的字符串属性(TotalPrice)的GUI更改,该属性只有一个getter,其演算涉及相关领域对象的属性。 这里有一些相关...

回答 1 投票 0

索引器属性的属性更改

我有一个带有索引器属性的类,带有字符串键: 公共类 IndexerProvider { 公共对象 this[字符串键] { 得到 { 返回 ... } 放 ...

回答 4 投票 0

在 SwiftUI 中的 Observable 对象中存储绑定

我正在创建一个Mac应用程序。当多次启动时,所有窗口都应访问相同的应用程序状态(theAppState),但每个窗口应显示自己的数据(MyClass 类型)。 就这样...

回答 1 投票 0

启用实时图表数据绑定时数据绑定停止工作

如果我取出启用 wpf 实时图表的代码,那么我的数据绑定代码可以正常工作,但是当显示图表时,到我的标签的数据绑定不再显示数据字符串。 我用的是

回答 1 投票 0

从 .NET MAUI 中的 viewModel 进行数据绑定的搜索处理程序

我有一个 .NET MAUI 移动应用程序,我正在尝试使用 ViewModel 的数据绑定来实现 SearchHandler。 我有下面的代码,但我收到“没有属性、BindableProperty 或事件发现...

回答 1 投票 0

如何在WPF中将List绑定到Listview userControl?

我正在尝试使用指定的 userControl UI 创建数据库中产品的 ListView。 这是我的清单。 我正在尝试使用指定的 userControl UI 创建数据库中产品的 ListView。 这是我的清单。 <ListView x:Name="ProductTable" Background="Transparent" ItemsSource="{Binding GetProducts}"> <ListView.ItemTemplate> <DataTemplate> <v:ProductLineStyle/> </DataTemplate> </ListView.ItemTemplate> <ItemsControl></ItemsControl> </ListView> 我尝试通过使用 XAML 代码中的 ItemSource 属性或在代码隐藏中将其绑定到我在代码隐藏中的列表。 ProductTable.Items.Clear(); ProductTable.ItemsSource = GetProducts(); 但我最后总是得到空列表项。 我检查了我想要绑定多次的列表,它不为空。 起初我尝试从ListView更改为ListBox,但它不起作用,我尝试使用userControl后面的代码作为产品类,但似乎userControl的构造函数不应该有参数,所以我使用了另一个上课,因为这样会更容易。 这就是我现在的用户控件中的代码 public ProductLineStyle() { InitializeComponent(); this.DataContext = this; } public string ProductID { get; set; } public string ProductName { get; set; } public int ProductPrice { get; set; } public int ProductQTé { get; set; } 这是 XAML 脚本 <Rectangle HorizontalAlignment="Stretch" RadiusX="10" RadiusY="10" Fill="#2A3E50"> </Rectangle> <DockPanel> <TextBlock x:Name="ID" Text="{Binding ProductID}" FontFamily="Inter Semi Bold" Margin="10" FontSize="20" Foreground="#01161E" VerticalAlignment="Center" Width="130" HorizontalAlignment="Left"/> <TextBlock x:Name="Product" Text="{Binding ProductName}" FontFamily="Inter Semi Bold" Margin="10" MaxWidth="400" FontSize="20" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Left"/> <TextBlock x:Name="Qte" Text="{Binding ProductQTé}" FontFamily="Inter Semi Bold" Width="50" FontSize="20" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Right" DockPanel.Dock="Right" Margin="0,0,10,0"/> <TextBlock x:Name="Price" Text="{Binding ProductPrice}" FontFamily="Inter Semi Bold" Margin="10,10,30,10" MaxWidth="400" FontSize="20" Foreground="White" VerticalAlignment="Center" DockPanel.Dock="Right" HorizontalAlignment="Right"/> </DockPanel> 这是我正在使用的课程。 internal class Product { public Product(string ID, string Name, int Price, int Qty) { ProductID = ID; ProductName = Name; ProductPrice = Price; ProductQTé = Qty; } public string ProductID { get; set; } public string ProductName { get; set; } public int ProductPrice { get; set; } public int ProductQTé { get; set; } } 您必须将ProductLineStyle内的DataTemplate控件与ItemsControl的项目连接起来。您必须将 ProductLineStyle 绑定到 Product 项目。 但是为了能够做到这一点,在 ProductLineStyle 控件中定义的所有属性都必须定义为依赖属性。不允许您将绑定分配给非依赖属性。 接下来,因为 Product 类是绑定源,所以它必须实现 INotifyPropertyChanged。如果不这样做,就会造成内存泄漏。 请勿对ItemsControl.ItemsSource进行操作。而是对源集合进行操作。除此之外, ItesControl.Items.Clear 调用是多余的,只会增加性能成本。它强制集合的完整迭代,以便逐项删除。此操作使用项目索引。这意味着,集合越大,清除集合所需的时间就越长。如果您打算更换集合,则不必事先清除它。 在 WPF 中,您无法绑定到方法。以下代码不起作用: <ListView x:Name="ProductTable" ItemsSource="{Binding GetProducts}" /> 您只能绑定到公共属性。 即使绑定有效,您也可以通过显式代码隐藏赋值来删除/覆盖它: // Overrides the binding declared in XAML ProductTable.ItemsSource = GetProducts(); 使用其中之一。更喜欢数据绑定。 还有另一个重要的细节:永远不要在内部设置控件的 DataContext。特别是当该控件需要在其一个或多个属性上定义 Binding 时。 这将破坏所有外部数据绑定: <Grid> <Grid.DataContext> <Product Id="Abc" /> </Grid.DataContext> <!-- Binding is unexpectedly broken and won't resolve, because the ProductStyle internally changes the value of DataContext: 'DataContext = this;' --> <ProductStyle ProductId="{Binding Id}" /> </Grid> 外部绑定通常针对当前DataContext。没有人预料到这个值会被控件默默地操纵。 关键是依赖属性引擎将自动从父级继承正确的 DataContext 值。 在您的场景中,该父项是 ListBoxItem,继承的值是 Product 项。现在您可以轻松地将 ProductStyle 控件绑定到当前 DataContext,即 Product。绑定将按预期运行 要修复代码,您必须更改类设计和实现。 产品.cs // This type must implement INotifyPropertyChanged. // If this type had been a child of DependencyObject // it would have to implement properties as dependency properties instead. internal class Product : INotifyPropertyChanged { // Use the common and recommend C# naming conventions // which states that fields and parameter names // must use camelCase. PascalCase is only for types and properties and events. public Product(string id) { this.ProductId = id; } protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => this.PropertyChanged?.Invoke(this, new PropertyChangedArgs(propertyName)); public string Id { get; set; } public event PropertyChangedEventHandler PropertyChanged; } MainWindow.xaml.cs partial class MainWindow : Window { public ObservableCollection<Product> Products { get; } public MainWindow() { InitializeComponent(); this.Products = new ObservableCollection<Product> { new Product("A112"), new Product("B543") }; } } MainWindow.xaml <Window> <ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Products}"> <ListBox.ItemTemplate> <ProductLineStyle ProductId="{Binding Id}" /> </ListBox.ItemTemplate> </ListBox> </Window> ProductStyle.xaml.cs class ProductLineStyle : UserControl { public string ProductId { get => (string)GetValue(ProductIdProperty); set => SetValue(ProductIdProperty, value); } public static readonly DependencyProperty ProductIdProperty = DependencyProperty.Register( "ProductId", typeof(string), typeof(MainWindow), new PropertyMetadata(default)); public ProductLineStyle() { InitializeComponent(); // Don't do this. This will break your external data bindings // (the data bindings defined on the ProductStyle element)! // The correct DataContext value will be automatically inherited from the parent. // In this scenario, the parent is the ListBoxItem and the value the Product type. //this.DataContext = this; } } 产品样式.xaml <UserControl> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ProductId}" /> </UserControl>

回答 1 投票 0

.net maui 中如何根据条件进行绑定?

我想问一下如何在.net Maui 应用程序中进行条件绑定。例如 如果我有课 公开课A级 { 公共字符串 Property1{get;set;} 公共字符串 Property2{get;set...

回答 2 投票 0

ASP.NET Core MVC 绑定问题

我正在尝试开发一个简单的博客应用程序来学习 ASP.NET Core 8 MVC。类别和帖子模型在多对多关系之间链接。当我尝试在多选中编辑类别时,我...

回答 1 投票 0

如何在自定义组合框控件的下拉列表末尾添加“AddNew”按钮?

我有一个带有组合框的自定义控件。我想在下拉列表的末尾添加一个按钮,上面写着“AddNew”,例如“AddNewStudent”,单击该按钮时,会打开一个表单

回答 1 投票 0

如何通过 MVVM 为 DataGrid ItemSource 设置过滤器

我有一个 DataGrid 绑定到 XAML 中的 CollectionViewSource。 我有一个 DataGrid 绑定到 XAML 中的 CollectionViewSource。 <Window.Resources> <local:MainWindowViewModel x:Key="ViewModel"/> <CollectionViewSource x:Key="cvsEntries" Source="{Binding LogEntriesStore, Source={StaticResource ViewModel}}"/> </Window.Resources> LogEntriesStore 是一个 ObservableCollection (LogEntry 是一个 DTO,在本次讨论中并不重要) DataGrid 声明为: <DataGrid AutoGenerateColumns="False" Margin="0" Name="dataGrid1" ItemsSource="{Binding Source={StaticResource cvsEntries}}" IsReadOnly="True"> 现在,我在这个 DataGrid 中的各个单元格上都有上下文菜单,以启动过滤请求。右键单击一个单元格,然后选择筛选器来筛选所有行,并仅显示此特定值。 MVVM 获取要过滤的请求,但现在是棘手的一点。如何在 CollectionViewSource 上设置过滤器? (顺便说一句 - 这就像在公园里用 Silverlight 散步一样轻松 PagedCollectionView,但 WPF 中似乎不提供这种功能,是吗?) 非常简单。您只需将集合视图移动到视图模型中即可: 在MainWindowViewModel中定义类型为ICollectionView的属性: public ICollectionView LogEntriesStoreView { get; private set; } 初始化 LogEntriesStore 属性后,您需要使用以下代码初始化 LogEntriesStoreView 属性: LogEntriesStoreView = CollectionViewSource.GetDefaultView(LogEntriesStore); 然后您需要从 XAML 中删除 CollectionViewSource 并修改 ItemsSource 绑定以指向新创建的集合视图属性: <DataGrid AutoGenerateColumns="False" Margin="0" Name="dataGrid1" ItemsSource="{Binding LogEntriesStoreView, Source={StaticResource ViewModel}}" IsReadOnly="True"> 就是这样。现在您可以访问视图模型内的集合视图,您可以在其中修改过滤器。 您的问题有多种解决方案,但在我看来,最好的解决方案是仅使用标准 WPF DataGrid 控件的样式,而不发明新的继承 DataGird 类型或依赖于其他第三方控件。以下是我找到的最佳解决方案: 选项 1:我个人使用:自动 WPF 工具包 DataGrid 过滤 选项 2:Microsoft WPF DataGrid 自动筛选 我还有一个问题。我也这样做。但是我现在如何处理 ObservableCollection 中模型的属性更改呢?无论如何,它不会在模具视图中刷新。

回答 3 投票 0

无法在 Blazor 项目中保存用户选择的值

我有一个应用程序,用户可以在其中选择我想要保存在 Json 文件中的设置。 首次呈现时,用户会获得一个包含设置名称和默认值的表。然后用户可以更改...

回答 1 投票 0

无法将从服务器接收到的 json 对象转换为所需类型,以便剖析数据

如果我问了太愚蠢的问题,我提前道歉,但我对 Angular 真的很陌生,我不明白如何处理来自服务器的 JSON 对象并将该对象转换为 c...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.