将两个数据/信息源绑定在一起并使它们保持同步的一般技术。
是否可以直接在 XAML 中与 Visual Studio REST 客户端服务代码生成器生成的对象进行双向数据绑定?
是否可以或实用将 XAML 中的 2 路数据绑定直接连接到基于 OpenApi 元数据文件的 Visual Studio 生成的 RESTApi。例如,Visual Studio 生成一个 REST 客户端,b...
我有一个弹出页面,我想从中返回一个值。所以在它的 c# 文件中,我的代码如下所示: 公共部分类 SelectDb : PopupPage { 任务完成源
Blazor InputSelect:无法使用 @bind-Value 将枚举值绑定到枚举类型
我正在使用 Blazor Web 应用程序。我有一个枚举。它具有三个值:UpperBody、LowerBody、FullBody。我的目标是让用户选择这些值之一。之后,程序将采用所选的
我是 xamarin mvvm patter 的初学者。目前,我正在尝试创建一个搜索栏,从名称列表中搜索单词。我尝试在我的视图模型上编写一些关于命令函数的代码,然后...
我的 WPF 应用程序生成的数据集每次可能具有不同的列数。 输出中包含对将用于应用格式设置的每列的描述。 一个
ASP.NET Core MVC:使用绑定将前端的格式化货币字符串转换为控制器中的十进制
我有一个这样的示例类: 公共类测试类 { [精度(18,2)] 公共十进制?金额{得到;放; } } 在前端,金额被视为具有以下格式的字符串...
NetCore 使用绑定将前端的格式化货币字符串转换为控制器中的小数
我有一个这样的示例类: 公共类测试类 { [精度(18,2)] 公共十进制?金额{获取;设置;} } 在前端,它以字符串形式威胁,示例格式如下:$120,555...
当子控件中的属性发生更改时,使用两种方式数据绑定在父控件中触发回调的推荐方法是什么? 我尝试过关注,但没有触发
如何按显示的、转换的值、未绑定源属性值对DataGridTextColumn进行排序?
如何按显示的、转换的值而不是绑定源属性值对 WPF DataGridTextColumn 进行排序?现在它按行视图模型中的整数值排序,而不是显示转换器返回的文本。我用...
我有一个自定义的用户控件下拉列表框,我需要用从数据库检索的一些列表项来填充它。 我的 ASPX 页面中的用户控件: 我有一个自定义的用户控件下拉列表框,我需要用从数据库检索的一些列表项来填充它。 我的 ASPX 页面中的用户控件: <tr> <td class="standard-label"> <asp:Label runat="server" AssociatedControlID="ddlchkProfession" Text="Professions:"></asp:Label> </td> <td class="messageAlignLeft"> <ucddtb:DropdownCheckboxList ID="ddlchkProfession" runat="server" /> </td> </tr> Ucontrol 的代码: <div class="form-group col-sm-5"> <asp:ListBox ID="ddlListBox" class="ddlchkBox" runat="server" SelectionMode="Multiple"> </asp:ListBox> </div> <script type="text/javascript"> $(function () { $('[id*=ddlListBox]').multiselect({ includeSelectAllOption: true, templates: { button: '<button type="button" class="multiselect dropdown-toggle" data-bs-toggle="dropdown"><span class="multiselect-selected-text"></span></button>' } }); }); </script> 我当前的逻辑背后的代码: try { List<FormLookup> fal = DBPR.ABT.Tax.BO.FormLookup.FormLookupGetList(DBPR.ABT.Tax.BO.BoBase.MasterConnectionString); foreach( FormLookup listItem in fal) { ListItem ddloption = new ListItem(); ddloption.Text = listItem.FormDescription; ddloption.Value = listItem.FormLookupId.ToString(); //this.ddlchkProfession.Controls.Add(ddloption); } } catch (Exception ee) { } 我正在尝试将 ListItems 添加到代码隐藏中的列表框,但我没有看到 ddlProfession 的任何选项在方法中执行任何此类数据绑定。我没有太多使用这些控件来实际保存数据的经验,所以我对如何构建它们以实际正确地包含数据有点迷失。非常感谢任何帮助。 更新:通过将我的 ListBox 指定为我的 ascx.cs 文件类中的 Listbox 类型,我能够实现我想要的目标... public ListBox ddlMultiSelectListBox { get { return ddlListBox; } } 然后我就可以从代码隐藏中访问它并绑定我的数据库结果,就像任何其他常规 ListBox 控件一样.. try { List<FormLookup> fal = FOO.BAR.Tax.BO.FormLookup.FormLookupGetList(foo.bar.Tax.BO.BoBase.MasterConnectionString); ddlchkProfession.ddlMultiSelectListBox.DataSource = fal; ddlchkProfession.ddlMultiSelectListBox.DataTextField = "FormDescription"; ddlchkProfession.ddlMultiSelectListBox.DataValueField = "formLookUpId"; ddlchkProfession.ddlMultiSelectListBox.DataBind(); } catch (Exception ee) { }
来自 XAML 中只读属性的 OneWayToSource 绑定
我正在尝试以 OneWayToSource 作为模式绑定到 Readonly 属性,但这似乎无法在 XAML 中完成: 我正在尝试以 Readonly 作为模式绑定到 OneWayToSource 属性,但似乎无法在 XAML 中完成: <controls:FlagThingy IsModified="{Binding FlagIsModified, ElementName=container, Mode=OneWayToSource}" /> 我得到: 无法设置属性“FlagThingy.IsModified”,因为它没有可访问的设置访问器。 IsModified 是 DependencyProperty 上的只读 FlagThingy。我想将该值绑定到容器上的 FlagIsModified 属性。 需要明确的是: FlagThingy.IsModified --> container.FlagIsModified ------ READONLY ----- ----- READWRITE -------- 仅使用 XAML 可以吗? 更新:好吧,我通过在容器上设置绑定而不是在FlagThingy上修复了这种情况。但我还是想知道这是否可能。 OneWayToSource 的一些研究成果... 选项#1。 // Control definition public partial class FlagThingy : UserControl { public static readonly DependencyProperty IsModifiedProperty = DependencyProperty.Register("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata()); } <controls:FlagThingy x:Name="_flagThingy" /> // Binding Code Binding binding = new Binding(); binding.Path = new PropertyPath("FlagIsModified"); binding.ElementName = "container"; binding.Mode = BindingMode.OneWayToSource; _flagThingy.SetBinding(FlagThingy.IsModifiedProperty, binding); 选项#2 // Control definition public partial class FlagThingy : UserControl { public static readonly DependencyProperty IsModifiedProperty = DependencyProperty.Register("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata()); public bool IsModified { get { return (bool)GetValue(IsModifiedProperty); } set { throw new Exception("An attempt ot modify Read-Only property"); } } } <controls:FlagThingy IsModified="{Binding Path=FlagIsModified, ElementName=container, Mode=OneWayToSource}" /> 选项#3(真正的只读依赖属性) System.ArgumentException:“IsModified”属性无法进行数据绑定。 // Control definition public partial class FlagThingy : UserControl { private static readonly DependencyPropertyKey IsModifiedKey = DependencyProperty.RegisterReadOnly("IsModified", typeof(bool), typeof(FlagThingy), new PropertyMetadata()); public static readonly DependencyProperty IsModifiedProperty = IsModifiedKey.DependencyProperty; } <controls:FlagThingy x:Name="_flagThingy" /> // Binding Code Same binding code... Reflector给出答案: internal static BindingExpression CreateBindingExpression(DependencyObject d, DependencyProperty dp, Binding binding, BindingExpressionBase parent) { FrameworkPropertyMetadata fwMetaData = dp.GetMetadata(d.DependencyObjectType) as FrameworkPropertyMetadata; if (((fwMetaData != null) && !fwMetaData.IsDataBindingAllowed) || dp.ReadOnly) { throw new ArgumentException(System.Windows.SR.Get(System.Windows.SRID.PropertyNotBindable, new object[] { dp.Name }), "dp"); } .... 这是 WPF 的限制,是设计使然。 Connect 上有报道: 来自只读依赖属性的 OneWayToSource 绑定 我制定了一个解决方案,可以动态地将只读依赖属性推送到名为 PushBinding 的源,我在 博客中提到了这里。下面的示例将 OneWayToSource 从只读 DP 的 ActualWidth 和 ActualHeight 绑定到 DataContext 的 Width 和 Height 属性 <TextBlock Name="myTextBlock"> <pb:PushBindingManager.PushBindings> <pb:PushBinding TargetProperty="ActualHeight" Path="Height"/> <pb:PushBinding TargetProperty="ActualWidth" Path="Width"/> </pb:PushBindingManager.PushBindings> </TextBlock> PushBinding 通过使用两个依赖属性(Listener 和 Mirror)来工作。侦听器已绑定 OneWay 到 TargetProperty,并在 PropertyChangedCallback 中更新 Mirror 属性,该属性已绑定 OneWayToSource 到 Binding 中指定的任何内容。 可以在这里下载演示项目。 它包含源代码和简短的示例用法。 写下这个: 用途: <TextBox Text="{Binding Text}" p:OneWayToSource.Bind="{p:Paths From={x:Static Validation.HasErrorProperty}, To=SomeDataContextProperty}" /> 代码: using System; using System.Windows; using System.Windows.Data; using System.Windows.Markup; public static class OneWayToSource { public static readonly DependencyProperty BindProperty = DependencyProperty.RegisterAttached( "Bind", typeof(ProxyBinding), typeof(OneWayToSource), new PropertyMetadata(default(Paths), OnBindChanged)); public static void SetBind(this UIElement element, ProxyBinding value) { element.SetValue(BindProperty, value); } [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)] [AttachedPropertyBrowsableForType(typeof(UIElement))] public static ProxyBinding GetBind(this UIElement element) { return (ProxyBinding)element.GetValue(BindProperty); } private static void OnBindChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ProxyBinding)e.OldValue)?.Dispose(); } public class ProxyBinding : DependencyObject, IDisposable { private static readonly DependencyProperty SourceProxyProperty = DependencyProperty.Register( "SourceProxy", typeof(object), typeof(ProxyBinding), new PropertyMetadata(default(object), OnSourceProxyChanged)); private static readonly DependencyProperty TargetProxyProperty = DependencyProperty.Register( "TargetProxy", typeof(object), typeof(ProxyBinding), new PropertyMetadata(default(object))); public ProxyBinding(DependencyObject source, DependencyProperty sourceProperty, string targetProperty) { var sourceBinding = new Binding { Path = new PropertyPath(sourceProperty), Source = source, Mode = BindingMode.OneWay, }; BindingOperations.SetBinding(this, SourceProxyProperty, sourceBinding); var targetBinding = new Binding() { Path = new PropertyPath($"{nameof(FrameworkElement.DataContext)}.{targetProperty}"), Mode = BindingMode.OneWayToSource, Source = source }; BindingOperations.SetBinding(this, TargetProxyProperty, targetBinding); } public void Dispose() { BindingOperations.ClearAllBindings(this); } private static void OnSourceProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.SetCurrentValue(TargetProxyProperty, e.NewValue); } } } [MarkupExtensionReturnType(typeof(OneWayToSource.ProxyBinding))] public class Paths : MarkupExtension { public DependencyProperty From { get; set; } public string To { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { var provideValueTarget = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); var targetObject = (UIElement)provideValueTarget.TargetObject; return new OneWayToSource.ProxyBinding(targetObject, this.From, this.To); } } 尚未在样式和模板中进行测试,猜测它需要特殊的外壳。 这是另一个基于 SizeObserver 的附加属性解决方案,详细信息请参见此处 将只读 GUI 属性推回 ViewModel public static class MouseObserver { public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached( "Observe", typeof(bool), typeof(MouseObserver), new FrameworkPropertyMetadata(OnObserveChanged)); public static readonly DependencyProperty ObservedMouseOverProperty = DependencyProperty.RegisterAttached( "ObservedMouseOver", typeof(bool), typeof(MouseObserver)); public static bool GetObserve(FrameworkElement frameworkElement) { return (bool)frameworkElement.GetValue(ObserveProperty); } public static void SetObserve(FrameworkElement frameworkElement, bool observe) { frameworkElement.SetValue(ObserveProperty, observe); } public static bool GetObservedMouseOver(FrameworkElement frameworkElement) { return (bool)frameworkElement.GetValue(ObservedMouseOverProperty); } public static void SetObservedMouseOver(FrameworkElement frameworkElement, bool observedMouseOver) { frameworkElement.SetValue(ObservedMouseOverProperty, observedMouseOver); } private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var frameworkElement = (FrameworkElement)dependencyObject; if ((bool)e.NewValue) { frameworkElement.MouseEnter += OnFrameworkElementMouseOverChanged; frameworkElement.MouseLeave += OnFrameworkElementMouseOverChanged; UpdateObservedMouseOverForFrameworkElement(frameworkElement); } else { frameworkElement.MouseEnter -= OnFrameworkElementMouseOverChanged; frameworkElement.MouseLeave -= OnFrameworkElementMouseOverChanged; } } private static void OnFrameworkElementMouseOverChanged(object sender, MouseEventArgs e) { UpdateObservedMouseOverForFrameworkElement((FrameworkElement)sender); } private static void UpdateObservedMouseOverForFrameworkElement(FrameworkElement frameworkElement) { frameworkElement.SetCurrentValue(ObservedMouseOverProperty, frameworkElement.IsMouseOver); } } 在控件中声明附加属性 <ListView ItemsSource="{Binding SomeGridItems}" ut:MouseObserver.Observe="True" ut:MouseObserver.ObservedMouseOver="{Binding IsMouseOverGrid, Mode=OneWayToSource}"> 这是绑定到 Validation.HasError 的另一个实现 public static class OneWayToSource { public static readonly DependencyProperty BindingsProperty = DependencyProperty.RegisterAttached( "Bindings", typeof(OneWayToSourceBindings), typeof(OneWayToSource), new PropertyMetadata(default(OneWayToSourceBindings), OnBinidngsChanged)); public static void SetBindings(this FrameworkElement element, OneWayToSourceBindings value) { element.SetValue(BindingsProperty, value); } [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)] [AttachedPropertyBrowsableForType(typeof(FrameworkElement))] public static OneWayToSourceBindings GetBindings(this FrameworkElement element) { return (OneWayToSourceBindings)element.GetValue(BindingsProperty); } private static void OnBinidngsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((OneWayToSourceBindings)e.OldValue)?.ClearValue(OneWayToSourceBindings.ElementProperty); ((OneWayToSourceBindings)e.NewValue)?.SetValue(OneWayToSourceBindings.ElementProperty, d); } } public class OneWayToSourceBindings : FrameworkElement { private static readonly PropertyPath DataContextPath = new PropertyPath(nameof(DataContext)); private static readonly PropertyPath HasErrorPath = new PropertyPath($"({typeof(Validation).Name}.{Validation.HasErrorProperty.Name})"); public static readonly DependencyProperty HasErrorProperty = DependencyProperty.Register( nameof(HasError), typeof(bool), typeof(OneWayToSourceBindings), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); internal static readonly DependencyProperty ElementProperty = DependencyProperty.Register( "Element", typeof(UIElement), typeof(OneWayToSourceBindings), new PropertyMetadata(default(UIElement), OnElementChanged)); private static readonly DependencyProperty HasErrorProxyProperty = DependencyProperty.RegisterAttached( "HasErrorProxy", typeof(bool), typeof(OneWayToSourceBindings), new PropertyMetadata(default(bool), OnHasErrorProxyChanged)); public bool HasError { get { return (bool)this.GetValue(HasErrorProperty); } set { this.SetValue(HasErrorProperty, value); } } private static void OnHasErrorProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.SetCurrentValue(HasErrorProperty, e.NewValue); } private static void OnElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue == null) { BindingOperations.ClearBinding(d, DataContextProperty); BindingOperations.ClearBinding(d, HasErrorProxyProperty); } else { var dataContextBinding = new Binding { Path = DataContextPath, Mode = BindingMode.OneWay, Source = e.NewValue }; BindingOperations.SetBinding(d, DataContextProperty, dataContextBinding); var hasErrorBinding = new Binding { Path = HasErrorPath, Mode = BindingMode.OneWay, Source = e.NewValue }; BindingOperations.SetBinding(d, HasErrorProxyProperty, hasErrorBinding); } } } xaml 中的用法 <StackPanel> <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"> <local:OneWayToSource.Bindings> <local:OneWayToSourceBindings HasError="{Binding HasError}" /> </local:OneWayToSource.Bindings> </TextBox> <CheckBox IsChecked="{Binding HasError, Mode=OneWay}" /> </StackPanel> 此实现特定于绑定 Validation.HasError WPF 不会使用 CLR 属性设置器,但似乎它会基于它进行一些奇怪的验证。 可能在你的情况下这可能没问题: public bool IsModified { get { return (bool)GetValue(IsModifiedProperty); } set { throw new Exception("An attempt ot modify Read-Only property"); } } 嗯...我不确定我是否同意这些解决方案。如何在属性注册中指定一个忽略外部更改的强制回调?例如,我需要实现一个只读 Position 依赖属性来获取 MediaElement 控件在用户控件内的位置。我是这样做的: public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(double), typeof(MediaViewer), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnPositionChanged, OnPositionCoerce)); private static void OnPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = d as MediaViewer; } private static object OnPositionCoerce(DependencyObject d, object value) { var ctrl = d as MediaViewer; var position = ctrl.MediaRenderer.Position.TotalSeconds; if (ctrl.MediaRenderer.NaturalDuration.HasTimeSpan == false) return 0d; else return Math.Min(position, ctrl.Duration); } public double Position { get { return (double)GetValue(PositionProperty); } set { SetValue(PositionProperty, value); } } 换句话说,只需忽略更改并返回由不具有 public 修饰符的不同成员支持的值。 -- 在上面的例子中,MediaRenderer实际上是私有的MediaElement控件。 我解决此限制的方法是在类中仅公开 Binding 属性,将 DependencyProperty 完全保持为私有。我实现了一个“PropertyBindingToSource”只写属性(这个属性不是 DependencyProperty),它可以设置为 xaml 中的绑定值。在此只写属性的设置器中,我调用 BindingOperations.SetBinding 将绑定链接到 DependencyProperty。 对于OP的具体示例,它看起来像这样: FlatThingy 实现: public partial class FlatThingy : UserControl { public FlatThingy() { InitializeComponent(); } public Binding IsModifiedBindingToSource { set { if (value?.Mode != BindingMode.OneWayToSource) { throw new InvalidOperationException("IsModifiedBindingToSource must be set to a OneWayToSource binding"); } BindingOperations.SetBinding(this, IsModifiedProperty, value); } } public bool IsModified { get { return (bool)GetValue(IsModifiedProperty); } private set { SetValue(IsModifiedProperty, value); } } private static readonly DependencyProperty IsModifiedProperty = DependencyProperty.Register("IsModified", typeof(bool), typeof(FlatThingy), new PropertyMetadata(false)); private void Button_Click(object sender, RoutedEventArgs e) { IsModified = !IsModified; } } 请注意,静态只读 DependencyProperty 对象是私有的。在控件中,我添加了一个按钮,其单击由 Button_Click 处理。 在我的 window.xaml 中使用 FlatThingy 控件: <Window x:Class="ReadOnlyBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ReadOnlyBinding" mc:Ignorable="d" DataContext="{x:Static local:ViewModel.Instance}" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Text="{Binding FlagIsModified}" Grid.Row="0" /> <local:FlatThingy IsModifiedBindingToSource="{Binding FlagIsModified, Mode=OneWayToSource}" Grid.Row="1" /> </Grid> 请注意,我还实现了一个 ViewModel 用于绑定到此处未显示的视图模型。它公开了一个名为“FlagIsModified”的 DependencyProperty,您可以从上面的源代码中收集到。 它工作得很好,允许我以松散耦合的方式将信息从 View 推回 ViewModel,并明确定义信息流的方向。 您现在的装订方向错误。 每当您正在创建的控件上的 IsModified 发生更改时,OneWayToSource 将尝试更新容器上的 FlagIsModified。 您想要相反的结果,即将 IsModified 绑定到 container.FlagIsModified。 为此,您应该使用绑定模式 OneWay <controls:FlagThingy IsModified="{Binding FlagIsModified, ElementName=container, Mode=OneWay}" /> 枚举成员的完整列表:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx
我想让我的申请变得流畅。但是,当我将窗口放大时,与 UI 元素相比,字体看起来很小。最终,当我
使用 ReactiveUI 在视图模型中绑定和操作用户控件属性的最佳实践是什么?
来自网络世界,您可以出于多种原因轻松创建可重用组件。许多可重用组件允许您操作从父级 c 中设置的数据属性...
我想要做的是在视图模型文件中创建和写入所有方法,并且仅将代码放置在自动生成的函数的代码隐藏文件中,例如
在实体框架中调用 SaveChanges() 时,WPF Datagrid 未更新
我有小型 WPF / .NET 应用程序,并通过 System.Data.Entity 连接到 SQLite 数据库。主窗口有一个按钮和一个文本框。 单击按钮时,必须添加一个带有 id 的新元素...
我试图在从巨大的数据库读取时实现分页。当用户移动到窗口外的页面时,我会获取下一组记录并填充表中的 9-10 页。问题是,之后
Avalonia:无法绑定到 ColumnDefinition.Width
我的 Avalonia 用户控件中有一个网格,当我的视图模型(派生自 ReactiveObject)的 bool 属性通过视图模型方法(代码在底部)设置为 true 时,我想隐藏其列。 S...
我的问题可能感觉很愚蠢,但我得到了这个模型: 导出接口角色{ id:号码; 角色名:字符串; 权限:字符串; 创建于:字符串; 更新时:字符串; } 导出接口...
我有一个 GridView,其中填充了最近 100 条文档记录(标题、描述、作者、创建日期)。 我使用 DataTable jQuery 插件来提供更多功能,例如:列拖放,
假设我有一个 Image,其 Source 来自绑定表达式,绑定到 URL(作为字符串): WPF 下载图像并显示它。 是