将两个数据/信息源绑定在一起并使它们保持同步的一般技术。
当我尝试将窗口的高度和宽度绑定到视图模型中的属性时遇到一些问题。这是一个小示例应用程序来说明该问题。这是app.xaml.xs中的代码 公开
我对 .net maui 和 c# 总体来说是个新手,我正在尝试编写一个单词游戏,我遇到的问题是在我的主页上使用数据绑定时出现了一些问题: 主要...
OnPropertyChanged 不会触发仅 getter 属性的 getter
我的目标是触发领域对象(Group)的字符串属性(TotalPrice)的GUI更改,该属性只有一个getter,其演算涉及相关领域对象的属性。 这里有一些相关...
我有一个带有索引器属性的类,带有字符串键: 公共类 IndexerProvider { 公共对象 this[字符串键] { 得到 { 返回 ... } 放 ...
在 SwiftUI 中的 Observable 对象中存储绑定
我正在创建一个Mac应用程序。当多次启动时,所有窗口都应访问相同的应用程序状态(theAppState),但每个窗口应显示自己的数据(MyClass 类型)。 就这样...
如果我取出启用 wpf 实时图表的代码,那么我的数据绑定代码可以正常工作,但是当显示图表时,到我的标签的数据绑定不再显示数据字符串。 我用的是
从 .NET MAUI 中的 viewModel 进行数据绑定的搜索处理程序
我有一个 .NET MAUI 移动应用程序,我正在尝试使用 ViewModel 的数据绑定来实现 SearchHandler。 我有下面的代码,但我收到“没有属性、BindableProperty 或事件发现...
如何在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>
我想问一下如何在.net Maui 应用程序中进行条件绑定。例如 如果我有课 公开课A级 { 公共字符串 Property1{get;set;} 公共字符串 Property2{get;set...
我正在尝试开发一个简单的博客应用程序来学习 ASP.NET Core 8 MVC。类别和帖子模型在多对多关系之间链接。当我尝试在多选中编辑类别时,我...
如何在自定义组合框控件的下拉列表末尾添加“AddNew”按钮?
我有一个带有组合框的自定义控件。我想在下拉列表的末尾添加一个按钮,上面写着“AddNew”,例如“AddNewStudent”,单击该按钮时,会打开一个表单
如何通过 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 中模型的属性更改呢?无论如何,它不会在模具视图中刷新。
我有一个应用程序,用户可以在其中选择我想要保存在 Json 文件中的设置。 首次呈现时,用户会获得一个包含设置名称和默认值的表。然后用户可以更改...
无法将从服务器接收到的 json 对象转换为所需类型,以便剖析数据
如果我问了太愚蠢的问题,我提前道歉,但我对 Angular 真的很陌生,我不明白如何处理来自服务器的 JSON 对象并将该对象转换为 c...
我有一个数据驱动的 Angular 应用程序。我有一个切换组件,我以切换状态传递该组件。我的问题是,除非我传入切换 bo,否则两种方式的数据绑定似乎不起作用...
弹出页面的数据绑定“未找到“Command”的属性、BindableProperty 或事件,或者值和属性之间的类型不匹配。”
我正在使用 .Net MAUI 开发移动应用程序,但在 XAML 中的数据绑定方面遇到问题。我有 MainPage.xaml,它从继承自 ObservableObject 的 MainPageViewModel.cs 接收数据。 在...
如何使用由 MappingName 填充的 SFDataGrid 将一列绑定到另一列
我试图根据同一行中另一列的布尔值来调整一列中的小数位数。 这是 XAML 我试图根据同一行中另一列的布尔值来调整一列中的小数位数。 这是XAML <syncf:GridCheckBoxColumn HeaderText="Headeer1" MappingName="OnlyIntegerQuantities" Width="120" AllowEditing="True"/> <syncf:GridNumericColumn HeaderText="Header2" MappingName="Quantity" Width="120" AllowEditing="True" Decimals="{Binding DataContext.OnlyIntegerQuantities,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=syncf:VirtualizingCellsControl}, Converter={StaticResource BooleanToDecimalConverter}}" ValidationMode="Decimal"/> 如果我绑定到 ViewModel 中的任何属性,它们工作正常,但我无法绑定到填充该行的模型中的属性。 我尝试绑定到 CellsControl 的相对源,但不起作用。 我正在使用 ObservableCollection 来填充数据网格的 ItemsSource。 使用GridTemplateColumn代替GridNumericColumn并使用内部的TextBlock定义模板。使用多值转换器的多重绑定设置 Text 属性。 <syncf:GridTemplateColumn HeaderText="Header2" MappingName="Quantity" Width="120" AllowEditing="True"> <syncf:GridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock> <TextBlock.Style> <Style TargetType="TextBlock"> <Setter Property="Text"> <Setter.Value> <MultiBinding Converter="{StaticResource BooleanToDecimalConverter}"> <Binding Path="Quantity"/> <Binding Path="OnlyIntegerQuantities"/> </MultiBinding> </Setter.Value> </Setter> </Style> </TextBlock.Style> </TextBlock> </DataTemplate> </syncf:GridTemplateColumn.CellTemplate> </syncf:GridTemplateColumn> 转换器 public class BooleanToDecimalConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var quantity = (double)values[0]; var onlyQuantityInteger = (bool)values[1]; if (onlyQuantityInteger) { return String.Format("{0:0}", quantity); } else { return (quantity).ToString(); } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
我想创建一个自定义组件,它允许通过以下(传统)设置进行双向绑定: // main.qml property var someStoredValue: someInitialValue // 可能是真实的 C++ 属性
我在 XAML 中有以下数据网格: 我在 XAML 中有以下数据网格: <DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" > <DataGrid.ColumnHeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="12" /> </Style> </DataGrid.ColumnHeaderStyle> <DataGrid.Columns> <DataGridTextColumn Header="Type" Width="200" FontSize="12" Binding="{Binding Path=Name}" /> <DataGridTemplateColumn Header="Ingredients" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <DataGrid ItemsSource="{Binding Ingredients}" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" > <DataGrid.ColumnHeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="12" /> </Style> </DataGrid.ColumnHeaderStyle> <DataGrid.Columns> <DataGridTextColumn Header="Ingredients" Width="*" FontSize="12" Binding="{Binding Path=IngredientName}"/> <DataGridTextColumn Header="Quantite" Width="*" FontSize="12" Binding="{Binding Path=Qty}"/> </DataGrid.Columns> </DataGrid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 我正在尝试找到一种动态创建数据网格(代码内)的方法,以便我可以创建它的多个副本并在运行时将其绑定到不同的数据源。 这可能吗?有人知道我该如何处理像这样复杂的数据网格吗? 首先,将尽可能多的不同设置移至可重用的 Styles 和 DataTemplates 中,在 DataGrid 本身中留下很少的内容: <UserControl ... > <UserControl.Resources> <Style x:Key="GridHeaderStyle" TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="12" /> </Style> <Style x:Key="ReadOnlyGridStyle" TargetType="{x:Type DataGrid}" > <Setter Property="AutoGenerateColumns" Value="False" /> <Setter Property="IsReadOnly" Value="True" /> <Setter Property="GridLinesVisibility" Value="None" /> <Setter Property="CanUserAddRows" Value="False" /> <Setter Property="CanUserDeleteRows" Value="False" /> <Setter Property="CanUserResizeColumns" Value="False" /> <Setter Property="CanUserResizeRows" Value="False" /> <Setter Property="CanUserReorderColumns" Value="False" /> <Setter Property="ColumnHeaderStyle" Value="{StaticResource GridHeaderStyle}" /> </Style> <DataTemplate x:Key="IngredientsCellTemplate"> <DataGrid ItemsSource="{Binding Ingredients}" Style="{StaticResource ReadOnlyGridStyle}"> <DataGrid.Columns> <DataGridTextColumn Header="Ingredients" Width="*" FontSize="12" Binding="{Binding Path=IngredientName}" /> <DataGridTextColumn Header="Quantite" Width="*" FontSize="12" Binding="{Binding Path=Qty}" /> </DataGrid.Columns> </DataGrid> </DataTemplate> </UserControl.Resources> <!-- A DataGrid using our Styles: --> <DataGrid ItemsSource="{Binding View}" Style="{StaticResource ReadOnlyGridStyle}" > <DataGrid.Columns> <DataGridTextColumn Header="Type" Width="200" FontSize="12" Binding="{Binding Path=Name}" /> <DataGridTemplateColumn Header="Ingredients" Width="*" CellTemplate="{StaticResource IngredientsCellTemplate}" /> </DataGrid.Columns> </DataGrid> </UserControl> 然后使用现有样式在代码隐藏中创建新的 DataGrid 会变得更加容易: var datagrid = new DataGrid(); datagrid.Style = FindResource("ReadOnlyGridStyle") as Style; datagrid.Columns.Add(new DataGridTextColumn() { Header = "Type", Width = new DataGridLength(200), FontSize = 12, Binding = new Binding("Name") }); datagrid.Columns.Add(new DataGridTemplateColumn() { Header = "Ingredients", Width = new DataGridLength(1, DataGridLengthUnitType.Star), CellTemplate = FindResource("IngredientsCellTemplate") as DataTemplate }); datagrid.ItemsSource = ...
“System.Windows.Data.Binding”类型的对象无法转换为“System.String”类型
我想知道是否有人可以帮忙。我已经在这个问题上敲了半天了,我一定是做错了什么。我有一个带有许多依赖属性的自定义控件。 [