binding 相关问题

这个标签在不同的环境中意味着不同的东西;考虑使用较少模糊的标签来代替或另外。常见含义包括:依赖项注入和数据绑定到对象和应用程序组件之间的绑定。

无法从 CollectionView 按钮点击 ICommand

在我使用 Mvvm.Toolkit 的应用程序中,我遇到了一个问题,即放置在 CollectionView 中的按钮在单击时无法调用 ViewModel 中关联的 ICommand 方法。

回答 1 投票 0

如何在 Angular 中定义值时仅在 HTML 元素中显示 id 属性

有这个简单的组件。有些用途需要输入字段上的 ID,有些则不需要。当值为假时,我不知道如何完全忽略 id 属性。这怎么可以……

回答 1 投票 0

什么是“符号”?

我正在研究框架和库 如果你看苹果文档档案馆的框架编程指南,“符号”这个词经常出现,但我不明白

回答 1 投票 0

正在寻找带有绑定的 findFragmentById 的替代方法?

有没有办法在不使用findFragmentById或findFragmentByTag的情况下获取对Fragment的引用?

回答 2 投票 0

“静态方法‘buildBlock’要求‘ToolbarItem<(), Button<some View>>’符合‘View’

静态方法“buildExpression”要求“ToolbarItem<(), Button>”符合“View” 公共结构SetAStatusView:查看{ @ObservedObject var 模型:StatusViewModel @环境(\.

回答 2 投票 0

WPF 用户控件 DataGrid 列绑定 C#

我想在 WPF 中创建一个 UserControl,其中有一个 DataGrid,并且我想像使用标准 DataGrid 一样使用它。 例如,我的用户控件如下所示: 我想在 WPF 中创建一个内部有 DataGrid 的 UserControl,并且我想像使用标准 DataGrid 一样使用它。 例如,我的 UserControl 显示如下: <UserControl x:Class="SupervisoreIB.UI.CustomControls.DataGridWithHeader" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ... mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="40"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Border Grid.Row="0"> <Label Content="test" /> </Border> <Border Grid.Row="1"> <DataGrid /> </Border> </Grid> </UserControl> 我想要的使用方式是这样的: <uc:DataGridWithHeader Grid.Row="1" Grid.Column="0" Margin="10"> <uc:DataGridWithHeader.Columns> <DataGridTextColumn Header="COL1" /> <DataGridTextColumn Header="COL2" /> <DataGridTextColumn Header="COL3" /> </uc:DataGridWithHeader.Columns> </uc:DataGridWithHeader> 我尝试像这样使用列依赖属性: public ObservableCollection<DataGridColumn> Columns { get { return (ObservableCollection<DataGridColumn>)GetValue(ColumnsProperty); } set { SetValue(ColumnsProperty, value); } } public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(ObservableCollection<DataGridColumn>), typeof(DataGridWithHeader), new PropertyMetadata(null, OnColumnsChanged)); 但似乎没有任何作用。 您应该在 UserControl 的构造函数中初始化集合属性: public partial class DataGridWithHeader : UserControl { public DataGridWithHeader() { InitializeComponent(); SetValue(ColumnsProperty, new ObservableCollection<DataGridColumn>()); } ... } 然后您还需要使用回调中的列填充实际的DataGrid,例如: private static void OnColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGridWithHeader dataGridWithHeader = (DataGridWithHeader)d; dataGridWithHeader.dataGrid.Columns.Clear(); if (dataGridWithHeader.Columns != null) foreach (DataGridColumn column in dataGridWithHeader.Columns) dataGridWithHeader.dataGrid.Columns.Add(column); }

回答 1 投票 0

将数据上下文字符串属性绑定到 StaticResource 键

我有一个带有 ResourceKey 和 Caption 的列表值,这些值都是字符串。 Resource是资源字典中定义的实际资源的名称。每个 ResourceKey Ico...

回答 3 投票 0

MAUI 将 FlyoutFooterTemplate 标签值设置为应用程序版本

我的 Shell 应用程序中的 FlyoutFooterTemplate DataTemplate 中有一个标签,我想将其设置为当前的应用程序版本 (AppInfo.Current.VersionString),但我很难尝试

回答 1 投票 0

C# WPF MVVM 图像源更新

我正在尝试从视图模型更新 xml 中的图像源,但似乎我无法做到这一点,而且我不明白我缺少什么。 所以...在视图 xml 中,我有一张图像,我为其制作了

回答 1 投票 0

(初学者)角度绑定问题

我刚刚开始编码。 我发现自己陷入了可能非常简单的事情。 我正在做书上的作业 它几乎没有告诉我该怎么做,但它不起作用。 (

回答 1 投票 0

在 Xamarin 中绑定 AAR 时删除样式属性

有没有办法在绑定 AAR 时删除对样式属性的引用?我的 AAR 在构建项目时会抛出错误,因为生成的 DLL 正在引用我的项目的样式属性...

回答 1 投票 0

在 WinUI 3 中的样式设置器中进行绑定

WinUI 3 是否支持样式设置器中的绑定?我已经为 NavigationView 定义了样式,第三行是: WinUI 3 支持样式设置器中的绑定吗?我已经为 NavigationView 定义了样式,第三行是: <Setter Property="CompactPaneLength" Value="{Binding CurrentCompactPaneLength}" /> 这会在运行时产生 Specified cast is not valid. 异常。包含 NavigationView 的页面的 DataContext 是该页面的 ViewModel。 NavigationView.CompactPaneLength 和 CurrentCompactPaneLength 都是 double 和 public,并且 CurrentCompactPaneLength 是一个 ObservableObject(来自 CommunityToolkit.Mvvm.ComponentModel)。 WinUI 3 (SDK 1.1.2) 的源代码包含各种 Setter,例如 <Setter Target="PaneContentGrid.Width" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CompactPaneLength}" /> 如果有必要的话,在代码中进行绑定是可行的。但 XAML 不应该也起作用吗? 显然,WinUI 3 尚不支持 Setter 中的常规绑定,尽管这是一项备受期待的功能。解决方法是创建一个包含 DependencyProperty 的帮助程序类,每当更改/设置属性时,该类都会调用更改处理程序。然后,更改处理程序可以在代码中创建所需的绑定。感谢 clemens,他很久以前就为 UWP 提出了类似的建议。这是一个辅助类示例: internal class BindingHelper { #region CompactPaneLengthBindingPath public static readonly DependencyProperty CompactPaneLengthBindingPathProperty = DependencyProperty.RegisterAttached( "CompactPaneLengthBindingPath", typeof(string), typeof(BindingHelper), new PropertyMetadata(null, BindingPathChanged)); public static string GetCompactPaneLengthBindingPath(DependencyObject obj) { return (string)obj.GetValue(CompactPaneLengthBindingPathProperty); } public static void SetCompactPaneLengthBindingPath(DependencyObject obj, string value) { obj.SetValue(CompactPaneLengthBindingPathProperty, value); } #endregion #region HeightBindingPath // another DP is defined here (all of them are strings) #region ForegroundBindingPath // and a third one, etc. // ===================== Change Handler: Creates the actual binding private static void BindingPathChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { if (e.NewValue is string source) // source property (could add DataContext by setting Value="source@datacontext" for example) { DependencyProperty target; // which property is the target of the binding? if (e.Property == CompactPaneLengthBindingPathProperty) target = NavigationView.CompactPaneLengthProperty; else if (e.Property == HeightBindingPathProperty) target = FrameworkElement.HeightProperty; else if (e.Property == ForegroundBindingPathProperty) target = Control.ForegroundProperty; else throw new System.Exception($"BindingHelper: Unknown target ({nameof(e.Property)}"); // don't know this property obj.ClearValue(target); // clear previous bindings (and value) BindingOperations.SetBinding(obj, target, // set new binding (and value) new Binding { Path = new PropertyPath(source), Mode = BindingMode.OneWay }); } } 请注意,所有 DependencyProperties 都是字符串类型,并且目标类型可以是您正在使用的控件的任何祖先类型。例如,HeightBindingPathProperty 绑定可以与任何 FrameworkElement 一起使用。 在 Style 中使用 helper 就像使用任何 Setter 一样,如下所示: <Style x:Key="MyNavigationView" TargetType="controls:NavigationView" > <Setter Property="local:BindingHelper.CompactPaneLengthBindingPath" Value="CurrentCompactPaneLength" /> </Style> 我希望这有帮助。 这对我有用: <Page.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="{x:Bind ViewModel.FontSize, Mode=OneWay}" /> </Style> </Page.Resources>

回答 2 投票 0

如何在不同的项目maui c#中绑定对象

有谁知道如何将对象绑定到不同的项目? 例如我有2个项目。一个是我的 maui 应用程序,其中包含 Mainpage.xaml,另一个是我想重用的 dll

回答 1 投票 0

在通知属性更改而不将新实例分配给属性时,Wpf 绑定不起作用

所以,我有这个非常简单的代码,当我在使用注释的代码行时单击按钮时,GUI 中的值通过绑定更新,一切正常。如果我使用另外两条线...

回答 1 投票 0

在日期输入元素(html 日期选择器)中将纪元日期时间显示为格式化日期

有角度 想要在日期输入元素(html日期选择器)中将纪元日期时间显示为格式化日期,使用绑定在输入中显示 我可以通过使用函数并在输入中设置来显示这一点,但想要...

回答 1 投票 0

在 Swift UI 中使用 TextFiled 同时更新两条记录

我使用文本文件,我可以在其中输入产品的数量。我正在使用 $Binding 属性包装器。这是购物车视图(订单视图)。问题是当我输入值时它会更新 b...

回答 1 投票 0

数据上下文冲突

<UserControl x:Class="WatermarkTextBox" 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" mc:Ignorable="d" d:DesignHeight="30" d:DesignWidth="250"> <UserControl.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </UserControl.Resources> <Border> <Grid x:Name="grid"> <TextBlock Text="{Binding Watermark, FallbackValue=This prompt dissappears as you type...}" Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" /> <TextBox Name="txtUserEntry" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" /> </Grid> </Border> </UserControl> 上面的代码显示了我的 WatermarkTextBox 控件。在文件后面的代码中,我设置了 DataContext。我省略了控件 DP 的所有代码。 public WatermarkTextBox() { InitializeComponent(); grid.DataContext = this; } 我必须将 DataContext 绑定到网格,否则水印和实际文本的 Text 属性将不会显示。现在的问题是我无法将Background的Border设置在Grid之外。 我尝试了下面的代码,但只设置了 Background 的 Border,而不是水印和实际文本。 public WatermarkTextBox() { InitializeComponent(); this.DataContext = this; grid.DataContext = this; } 在这样的 UserControl 中,您永远不应该将 DataContext 显式设置为 this 或其他任何值,因为当您在应用程序中的某个位置使用 UserControl 时,DataContext 通常是在外部设置的。外部应用的 DataContext 通常是应用程序视图模型(的一部分)。 您应该更改内部绑定,以便它们使用显式 RelativeSource: <TextBlock Text="{Binding Path=Watermark, RelativeSource={RelativeSource AncestorType=UserControl}, FallbackValue=This prompt dissappears as you type...}" Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" /> <TextBox Name="txtUserEntry" Text="{Binding Path=Text, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=UserControl}}" /> 然后从 UserControl 的构造函数中删除任何 DataContext 赋值。 参见例如这个答案(以及许多其他类似的答案)详细讨论了这个主题。 @Clemens 答案的附录: 为了避免为每个控件设置 RelativeSource={RelativeSource AncestorType=UserControl},请在用户控件中提供一个根元素来获取 DataContext。 <UserControl x:Class="UserControlBindingIssue.TestUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:UserControlBindingIssue" d:DataContext="{d:DesignInstance Type=local:TestUserControl, IsDesignTimeCreatable=True}" mc:Ignorable="d"> <ContentControl DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:TestUserControl}}}"> <TextBox Width="100" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" /> <!-- Other controls --> </ContentControl> </UserControl>

回答 2 投票 0

Pythonnet - 内存管理错误

环境 Pythonnet版本:3.0.2 Python版本:3.9 操作系统:Windows 10 .NET 运行时:.NET Framework 4.8 细节 我正在尝试通过 .NET Framework DLL 打开一个大文件并...

回答 1 投票 0

XAML 中的绑定中是否可以仅包含边距的一部分

我有一个XAML,包含如下信息: ...保证金=“10,20,30,40”... 我知道在绑定中设置左边距非常容易,如下所示: Margin="{绑定margin_value...

回答 1 投票 0

MAUI CollectionView 中的绑定字符串问题

我创建了一个名为SectionTitle 的.net MAUI 组件,它有两个属性:一个用于背景颜色,一个用于标签字符串。 我创建了一个名为 SectionTitle 的 .net MAUI 组件,它有两个属性:一个用于背景颜色,一个用于标签字符串。 <?xml version="1.0" encoding="utf-8" ?> <ContentView x:Class="mater.Components.SectionTitle" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:c="clr-namespace:mater.Components" x:DataType="c:SectionTitle"> <Grid ColumnDefinitions="3*,2*"> <Grid ColumnDefinitions="Auto,*"> <Border x:Name="Bordo" Grid.Column="0" Padding="5,10,20,10" Background="{Binding BGColor}" StrokeShape="RoundRectangle 0,10,0,10"> <Label x:Name="Lbl" Grid.Column="0" Text="{Binding LabelText}" /> </Border> </Grid> </Grid> </ContentView> 对应的c#代码如下: namespace mater.Components; public partial class SectionTitle : ContentView { public static readonly BindableProperty BGColorProperty = BindableProperty.Create( nameof(BGColor), typeof(Brush), typeof(SectionTitle), default(Brush)); public static readonly BindableProperty LabelTextProperty = BindableProperty.Create( nameof(LabelText), typeof(string), typeof(SectionTitle), ""); public Brush BGColor { get { return (Brush)GetValue(BGColorProperty); } set { SetValue(BGColorProperty, value); } } public string LabelText { get { return (string)GetValue(LabelTextProperty); } set { SetValue(LabelTextProperty, value); } } public SectionTitle() { InitializeComponent(); BindingContext = this; } } 在 CollectionView 中使用它时,直接使用模型的 Label 属性时绑定可以工作,但当数据通过我的 SectionTitle 组件传递时它不起作用。为 LabelText 设置静态值是有效的。 <?xml version="1.0" encoding="UTF-8" ?> <ContentPage x:Class="mater.Views.HygienePage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:c="clr-namespace:mater.Components" xmlns:local="clr-namespace:mater" xmlns:models="clr-namespace:mater.Models" xmlns:vm="clr-namespace:mater.ViewModels" x:DataType="vm:HygieneViewModel"> <ScrollView> <StackLayout Margin="0,10,0,0"> <CollectionView Margin="20,10,20,10" ItemsSource="{Binding Cards}" SelectionChanged="CollectionView_SelectionChanged" SelectionMode="Single"> <CollectionView.ItemsLayout> <GridItemsLayout HorizontalItemSpacing="20" Orientation="Vertical" Span="2" VerticalItemSpacing="20" /> </CollectionView.ItemsLayout> <CollectionView.ItemTemplate> <DataTemplate x:DataType="models:CardItem"> <StackLayout Margin="0,0,0,10" Background="{StaticResource LightGrey}"> <Grid ColumnDefinitions="Auto,*"> <Border Grid.Column="0" Margin="0,10,0,0" Padding="5,10,20,10" Background="{StaticResource Coral}" StrokeShape="RoundRectangle 0,10,0,10"> <Label Text="{Binding Label}" /> </Border> <Grid Grid.Column="1" /> </Grid> <!-- Here if I put LabelText="ASD" it works, with the binding it doesn't work, the binding 3 lines before works --> <c:SectionTitle BGColor="{StaticResource Coral}" LabelText="{Binding Label}" /> <Image HeightRequest="150" Source="{Binding Icon}" /> </StackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </StackLayout> </ScrollView> </ContentPage> 为了完整性,我还添加了相应的 C# 代码 using mater.ViewModels; using mater.Models; using mater.Resources.Strings; namespace mater.Views; public partial class HygienePage : ContentPage { public List<CardItem> Cards = new() { new CardItem { Icon="rischi_micro.png", Label=AppStrings.SicRischi }, new CardItem { Icon="da_evitare.png", Label=AppStrings.SicEvitare }, new CardItem { Icon="acquisto.png", Label=AppStrings.SicLavaggio }, new CardItem { Icon="cottura.png", Label=AppStrings.SicCottura }, new CardItem { Icon="contaminazioni.png", Label=AppStrings.SicContaminazioni }, }; public HygieneViewModel vm { get; set; } public HygienePage() { InitializeComponent(); vm = new HygieneViewModel(); vm.Cards = Cards; BindingContext = vm; } async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.CurrentSelection.Count > 0) { CardItem selectedCard = (CardItem)e.CurrentSelection[0]; ((CollectionView)sender).SelectedItem = null; switch (selectedCard.Label) { case var value when value == AppStrings.SicRischi: await Shell.Current.GoToAsync("rischimicropage"); break; case var value when value == AppStrings.SicEvitare: await Shell.Current.GoToAsync("evitarepage"); break; case var value when value == AppStrings.SicLavaggio: await Shell.Current.GoToAsync("lavaggiopage"); break; case var value when value == AppStrings.SicCottura: await Shell.Current.GoToAsync("cotturapage"); break; case var value when value == AppStrings.SicContaminazioni: await Shell.Current.GoToAsync("contaminazionipage"); break; default: break; } } } } CardItem 模型: using System; namespace mater.Models; public class CardItem { public ImageSource Icon { get; set; } public string Label { get; set; } public bool isVisible { get; set; } = true; } 和视图模型: using System; using CommunityToolkit.Mvvm.ComponentModel; using mater.Models; namespace mater.ViewModels; public partial class HygieneViewModel: ObservableObject { [ObservableProperty] private List<CardItem> cards; } 知道我的绑定做错了什么吗? 正如 @Jason 在评论中强调的那样,问题是在控件中使用 BindingContext=this 。 我从 C# 的构造函数中删除了 BindingContext = this; 向控件添加了 x:Name,并且绑定源更新如下: <?xml version="1.0" encoding="utf-8" ?> <ContentView x:Class="mater.Components.SectionTitle" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:c="clr-namespace:mater.Components" x:Name="self" x:DataType="c:SectionTitle"> <Grid ColumnDefinitions="3*,2*"> <Grid ColumnDefinitions="Auto,*"> <Border x:Name="Bordo" Grid.Column="0" Padding="5,10,20,10" Background="{Binding Source={x:Reference self}, Path=BGColor}" StrokeShape="RoundRectangle 0,10,0,10"> <Label x:Name="Lbl" Grid.Column="0" Text="{Binding Source={x:Reference self}, Path=LabelText}" /> </Border> </Grid> </Grid> </ContentView> 现在一切正常

回答 1 投票 0

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