data-binding 相关问题

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

ViewModel 中的 IFormFile 属性未绑定

当我尝试发布包含 List 属性的 QuizCreate 视图模型时,它没有绑定并且始终为 NULL。 我有一个 POST 表单 QuizCreate.cs 的视图模型: 公开课 QuizCreat...

回答 1 投票 0

通过数据绑定属性动态创建的WPF UserControl

我正在尝试创建 WPF UserControl,其设计应基于某些数据绑定属性。 简单的例子我想实现什么: 让我们拥有包含我的控件的窗口: ...

回答 1 投票 0

将 ComboBox ItemsSource 和 SelectedItem 绑定到不同的 DataContexts

在我的应用程序中,我有一个对象 MaterialLibrary,它保存各种材质名称的列表,以及一个对象 UserInputData,它保存各种用户输入。 MaterialLibrary 和 UserInputData 是 和

回答 1 投票 0

将视图模型属性绑定到自定义视图中的属性

我创建了一个具有步骤属性的自定义视图(NavProgressbar)。 私有静态只读 BindableProperty ProgressStepProperty = BindableProperty.Create( 名称(进度步骤),类型...

回答 4 投票 0

如何将 Api 中的值绑定到 Angular 表单

我创建了一个角度表单,它将值发送到 webapi 并在提交时在表单下方打印收到的值。 Try.component.html: 我创建了一个角度表单,它将值发送到 webapi 并在提交时在表单下方打印收到的值。 尝试.component.html: <div class="container"> <form [formGroup]="form" (submit)="onSubmit()" class="mb-3"> <!-- <form class="mb-3"> --> <div class="mb-3" class="row"> <label for="bike" class="form-label fw-bold">Bike:</label> <input type="text" class="form-control" id="bike" formControlName="bike" placeholder="Enter your Bike name"> </div> <button type="submit" class="btn btn-primary mr-1">Submit</button> </form> <br> <br> <div class="table-responsive"> <table class="table table-bordered" role="grid"> <thead class="bg-primary"> <tr> <th>Bike Name</th> </tr> </thead> <tbody> <tr *ngFor="let name of names"> <td> <input type="text" [(ngModel)]="name.bike" name="bike"> </td> </tr> </tbody> </table> </div> </div> 尝试.component.ts: export class TryComponent { names: any[] = []; form: FormGroup; constructor(private fb: FormBuilder, private namesService: NamesService) { this.form = this.fb.group({ bike: ['', [Validators.required, Validators.minLength(3)]] }); this.namesService.getBikes(); } Reset(){ this.form.reset(); } onSubmit() { this.form.controls['bike'].markAsTouched(); if (this.form.valid) { const data={ ...this.form.value, } console.log(data); this.namesService.addBike(data).subscribe(response => { console.log(response); this.namesService.getBikes().subscribe(names => { this.names = names; console.log(this.names); console.log("success"); }); }); } } } 名称.service.ts: export class NamesService { constructor(private http: HttpClient) { } addBike(nameData : any): Observable<any>{ return this.http.post<any[]>('https://localhost:7006/api/Names/submit-form',nameData); } getBikes(): Observable<any[]> { return this.http.get<any[]>('https://localhost:7006/api/Names/get-form-data'); } } 问题不在于 API,因为我可以在控制台上将输入的值作为数组打印(使用 swagger 测试时效果很好)。即使在导入必要的模块后,我也无法绑定该值和将其显示在表单下方。我想问题在于数组没有用前端接收到的值或我绑定的方式更新。 有人可以告诉我我哪里出了问题吗? 按如下方式修改您的TryComponent: import { Component, OnInit } from '@angular/core'; // Import OnInit // ... other imports ... export class TryComponent implements OnInit { // Implement OnInit names: any[] = []; form: FormGroup; constructor(private fb: FormBuilder, private namesService: NamesService) { this.form = this.fb.group({ bike: ['', [Validators.required, Validators.minLength(3)] }); } ngOnInit() { this.namesService.getBikes().subscribe(names => { this.names = names; }); } // ... rest of your component code ... } 通过实现 OnInit,您可以确保在初始化组件时进行 getBikes() 调用。 在 names 方法中成功添加自行车后,请务必更新 onSubmit() 数组: onSubmit() { this.form.controls['bike'].markAsTouched(); if (this.form.valid) { const data = { ...this.form.value, }; this.namesService.addBike(data).subscribe(response => { console.log(response); this.namesService.getBikes().subscribe(names => { this.names = names; // Update the names array console.log(this.names); console.log("success"); }); }); } }

回答 1 投票 0

WPF 密码框提示不起作用

我正在尝试在WPF中设置PasswordBox的样式,以便它可以显示提示。 我通过在 ResourceDictionary 中使用以下代码来执行此操作: 我正在尝试在WPF中设置PasswordBox的样式,以便它可以显示Hint。 我通过在 ResourceDictionary 中使用以下代码来完成此操作: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BudgetBuddy.Styles"> <Style x:Key="PBHintStyle" TargetType="{x:Type PasswordBox}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="BorderBrush" Value="#FFABADB3"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="True"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type PasswordBox}"> <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="4" Padding="5 2 0 0"> <Grid> <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> <TextBlock x:Name="WARKTEXT" Text="{TemplateBinding Tag}" Foreground="DimGray" Visibility="Collapsed" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" TargetName="border" Value="0.56"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsFocused" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Visibility" TargetName="WARKTEXT" Value="Visible"/> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsInactiveSelectionHighlightEnabled" Value="True"/> <Condition Property="IsSelectionActive" Value="False"/> </MultiTrigger.Conditions> <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> </MultiTrigger> </Style.Triggers> </Style> </ResourceDictionary> 这段代码可以工作,但有一个问题。它显示提示,将字符显示为密码字符,但问题是在字段中输入密码后,它会在密码字符上显示提示。 以下是一些截图: 问题是这样的: 我该如何解决这个问题?提前致谢。 :) 您可以尝试处理PasswordBox的PasswordChanged事件,并根据Password属性是否包含任何字符将其Tag属性设置为“True”或“False”,然后检查触发器中Tag属性的值 在 xaml 上: <Style x:Key="PasswordStyle" TargetType="{x:Type PasswordBox}"> <Setter Property="Tag" Value="False"/> <EventSetter Event="PasswordChanged" Handler="OnPasswordChanged"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type PasswordBox}"> <Border x:Name="border" CornerRadius="4" BorderBrush="LightGray" BorderThickness="4" Background="Transparent" SnapsToDevicePixels="True"> <Grid> <TextBlock x:Name="PassMessage" HorizontalAlignment="Left" VerticalAlignment="Center" Text=" Password..." FontSize="34" Margin="5,0,5,0" Foreground="LightGray" IsHitTestVisible="False" Visibility="Hidden"/> <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" BorderBrush="Transparent" VerticalScrollBarVisibility="Hidden"/> </Grid> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Tag" Value="False"/> <Condition Property="IsKeyboardFocusWithin" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Visibility" TargetName="PassMessage" Value="Visible"/> </MultiTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Grid> <PasswordBox x:Name="passwordBox" Style="{StaticResource PasswordStyle}" HorizontalAlignment="Left" Margin="217,256,0,0" VerticalAlignment="Top" Width="450"/> </Grid> 在 xaml.cs 上 private void OnPasswordChanged(object sender, RoutedEventArgs e) { passwordBox = sender as PasswordBox; passwordBox.Tag = (!string.IsNullOrEmpty(passwordBox.Password)).ToString(); } 希望这有帮助 你可以试试这个: <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsFocused" Value="False"/> <Condition Property="Text" Value=""/> </MultiTrigger.Conditions> <Setter Property="Visibility" TargetName="WARKTEXT" Value="Visible"/> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsFocused" Value="False"/> <Condition Property="Text" Value="{x:Null}"/> </MultiTrigger.Conditions> <Setter Property="Visibility" TargetName="WARKTEXT" Value="Visible"/> </MultiTrigger> 如果文本不为空或为空,则会显示提示,否则不显示。 希望对您有帮助。

回答 2 投票 0

如何在 WPF 中的 XML 数据绑定列表框中添加/删除项目?

我是 WPF 新手,XML 数据绑定让我很困惑,所以如果我不知道我在说什么,请耐心等待。 我正在尝试创建一个具有双向绑定的 XML 绑定列表框。我当时...

回答 1 投票 0

WPF:如何将列表中的数据绑定到树视图节点内的组合框

我正在尝试将 UserLevelCategories 列表中的数据绑定到 Treeview 中的 ComboBox。 **UserLevelCategories **列表位于另一个列表 - systemBranches 内。 模型: 公开课

回答 1 投票 0

自定义依赖属性找不到管理 FrameworkElement 或 FrameworkContentElement 异常

我正在开发一个派生自 TextBox 的 UserControl。 我有自定义 DependencyProperty HelperText,如下所示: 公共静态只读 DependencyProperty HelperTextProperty = DependencyProperty.Regist...

回答 1 投票 0

如何将多个控件绑定到 .NET maui 中的单个数据源?

我正在开发一个项目,它类似于在 maui 社区工具包的帮助下使用 MVVM 方案的购物车。我有一个可观察集合中的项目列表,点击/cl...

回答 1 投票 0

如何在DataTemplate中绑定两个不同的类属性

我正在尝试绑定 DataTemplate 中不同类的两个属性。 我正在尝试绑定 DataTemplate 中不同类的两个属性。 <DataTemplate x:Key="DemoItemTemplate" x:DataType="local:DemoInfo"> <NavigationViewItem Visibility="{Binding Visibility, Mode=TwoWay}" Content="{x:Bind Name}"/> </DataTemplate> 将此 DataType 的 DemoInfo 设置为 DataTemplate,并从 Name 更新 DemoInfo 值。 我尝试过将视图模型视为源和相对源绑定。但是 Visibility 属性绑定在 ViewModel 类中不起作用。有什么建议如何实现这一目标吗? Visibility="{Binding Visibility, Source={StaticResource viewModel}}" AFAIK,你不能在 UWP 中使用多重绑定,你可以尝试使用 Locator 什么是 ViewModelLocator,与 DataTemplates 相比,它的优点/缺点是什么? 如何在DataTemplate中绑定两个不同的类属性 如果您将 Visibility 与 StaticResource 绑定,请在页面 Resources 中声明 ViewModel 类,如下所示。 视图模型 public class ViewModel { public ViewModel() { Visibility = false; } public bool Visibility { get; set; } } Xaml <Page.Resources> <local:ViewModel x:Key="ViewModel" /> </Page.Resources> <DataTemplate x:DataType="local:Item"> <TextBlock Width="100" Height="44" Text="{x:Bind Name}" Visibility="{Binding Visibility, Source={StaticResource ViewModel}}" /> </StackPanel> </DataTemplate> 更新 如果您希望 Visibility 值在运行时动态更改,您需要为 ViewModel 类实现 INotifyPropertyChanged 接口。 public class ViewModel : INotifyPropertyChanged { public ViewModel() { Visibility = false; } private bool _visibility; public bool Visibility { get { return _visibility; } set { _visibility = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string PropertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); } } 更多详情请参考深度数据绑定官方文档 我可能是错的,但我认为 Visibility 属性有一个专用枚举,其中包含 Visibility 属性的所有可能选项(Visibility Enum)。因此,您的绑定可能工作得很好,但绑定属性的 Type 需要使用 Visibility 为 System.Windows 类型。 顺便说一句,我无论如何都不会在视图模型中添加可见性属性。我认为更标准的方法是在绑定视图后面的直接代码中具有可见性DependencyProperty。

回答 3 投票 0

Value 和@bind-Value 之间的区别?

我正在查看InputCheckBox的文档https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.inputcheckbox,我发现它公开了要绑定的值到 desi...

回答 2 投票 0

通过 ICommand 更新后 WPF TreeView 结构丢失

我正在开发一个WPF项目,目前它实现了ComboBox、Button、ICommand和TreeView。 TreeView 是通过绑定到 Button 的 ICommand 接口生成的。跑步时...

回答 1 投票 0

为什么列表框中不显示列表数据?

我几天前开始编程,但我一直在试图找出为什么我的代码不起作用。非常感谢任何帮助! 这是我的代码: ... 公共部分类 Form1 :表格 { ...

回答 1 投票 0

有没有办法在 SwiftUI 中“解除绑定”变量/对象

我能做的最好的比较是所有手机都附带的 iPhone 通讯录应用程序。 当用户启动应用程序时,它会列出所有联系人。用户可以选择联系人和“详细信息...

回答 2 投票 0

Angular 版本:14.2.9 - “表单数据绑定问题:数据未正确绑定/填充 - 需要帮助”

我目前正在使用 Angular 版本 14.2.9,在将数据绑定到表单时遇到问题。我想实现一个功能,在单击“更新”图标时,模式会打开...

回答 1 投票 0

WPF 复合集合

我正在浏览WPF官方文档How to: Implement a CompositeCollection,对CollectionContainer有一些疑问。 我正在浏览WPF官方文档,如何:实现CompositeCollection,对CollectionContainer有一些疑问。 <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{StaticResource greekGods}" /> <CollectionContainer Collection="{Binding Source={StaticResource greekHeroes}}" /> <ListBoxItem Background="red">Other ListBox Item 1</ListBoxItem> <ListBoxItem Background="red">Other ListBox Item 2</ListBoxItem> </CompositeCollection> </ListBox.ItemsSource> 为什么 XMLDataProvider 资源 GreekHeroesData 必须使用 Binding?否则会抛出异常。 <CollectionContainer Collection="{Binding Source={StaticResource greekHeroes}}" /> 但是CLR资源GreekGodsData可能会忽略它? <CollectionContainer Collection="{StaticResource greekGods}" /> 感谢您的帮助! 当我写下以下内容时: <CollectionContainer Collection="{StaticResource greekHeroes}" /> 它引发了异常: System.Windows.Markup.XamlParseException HResult=0x80131501 Message=“An exception was thrown while setting the property 'System.Windows.Data.CollectionContainer.Collection'.”, the line number is "49", and the line position is "22". Source=PresentationFramework StackTrace: at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at CompositeCollections.MainWindow.InitializeComponent() in I:\github.com\learning-wpf\DataBinding\CompositeCollections\MainWindow.xaml : line 1 This exception was initially thrown in this call stack: [external code] Internal exception 1: ArgumentException: 'System.Windows.Data.XmlDataProvider' is not a valid value for property 'Collection'. 感谢您的帮助! 为什么 XMLDataProvider 资源 GreekHeroesData 必须使用 Binding? 正如您在文档链接中看到的,在他们的 XAML 中,他们定义了 [XmlDataProvider][1],这意味着您可以在 XAML 文件而不是视图模型中创建数据 -> 因此 GreekHeroesData 必须使用 Binding CLR 资源 GreekGodsData 可能会忽略它? GreekGodsData他们从另一个资源filfe(clr-namespace:SDKSample)获取,因此需要使用静态资源来获取它。 当我用以下内容编写时,它引发了异常? 正如我上面的解释,greekHeroes 是由 XmlDataProvider 定义的,因此您必须是您Binding才能获取其数据。 [1]:https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.xmldataprovider?view=windowsdesktop-7.0

回答 1 投票 0

DataBinding DateTimePicker 引发“DataBinding 无法在列表中找到适合所有绑定的行。”

我有一个简单的测试应用程序,它重现了我最近遇到的错误。基本上我有一个简单的 WinForm,带有数据绑定 TextBox 和 DateTimePicker 控件以及一个按钮。当我执行时...

回答 1 投票 0

当我将相同的属性绑定到另一个ListView时,ListView保持为空

如果我将相同的属性绑定到相同类型的另一个控件,在我的例子中绑定到 ListView,那么第一个 ListView 将填充对象,但第二个 ListView 保持为空。我和 On 一起玩...

回答 1 投票 0

WinForm TreeView 节点为空,尽管有一个数据源到对象关系列表

在我的 Telerik WinForms 应用程序中,我可以通过对象关系绑定将 TreeView 绑定到应用程序中的 Well 对象,如下所示: this.wellTreeView = new RadTreeView(); 绑定列表<...

回答 2 投票 0

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