这个标签在不同的环境中意味着不同的东西;考虑使用较少模糊的标签来代替或另外。常见含义包括:依赖项注入和数据绑定到对象和应用程序组件之间的绑定。
WPF DataGrid 不使用 DataTemplate 和 DataTriggers 显示数据
我有ViewModel 公共类视图模型 { 公共字符串测试字段{获取;放; } 公共 ObservableCollection TestCollection { 获取;放; } 公共视图模型() { ... 我有视图模型 public class ViewModel { public string TestField { get; set; } public ObservableCollection<object> TestCollection { get; set; } public ViewModel() { TestField = "ABCD"; } } 我想根据 TestField 的值使用不同的 DataGridTemplateColumns。 DataGrid 将 TestCollection 作为其 ItemSource,并且在 DataTemplate 中,我想使用 ItemSource 中的项目。不过DataTemplate的选择需要依赖于TestField。 问题在于 TestField 位于 TestCollection 的上下文之外。 一种解决方案是使用转换器将 TestField 属性绑定到 CellTemplate 属性。 如果 TestField 属性在创建 ViewModel 实例后可以更改,则需要通过 INotifyPropertyChanged.PropertyChanged 事件提供其更改的通知。在我的示例中,这是通过 ViewModelBase 基类的方法完成的: public class TestFieldViewModel : ViewModelBase { public string TestField { get => Get<string>(); set => Set(value); } public ObservableCollection<object> TestCollection { get; /*set;*/ } = new(); public TestFieldViewModel() { foreach (var item in "Example") { TestCollection.Add(item); } // асинхронная смена значения с целью тестирования Task.Run(async () => { while (true) { await Task.Delay(2000); TestField = TestField == "ABCD" ? string.Empty : "ABCD"; } }); } } public class TestFieldTemplateConverter : IValueConverter { public DataTemplate Default { get; set; } public DataTemplate ABCD { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value switch { "ABCD" => ABCD, _ => Default }; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } <Window ---------------------------- ---------------------------- DataContext="{DynamicResource vm}"> <Window.Resources> <local:TestFieldViewModel x:Key="vm"/> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding TestCollection}"> <DataGrid.Resources> <DataTemplate x:Key="default"> <TextBlock Text="Default"/> </DataTemplate> <DataTemplate x:Key="ABCD"> <TextBlock Text="ABCD"/> </DataTemplate> <local:TestFieldTemplateConverter x:Key="templateConverter" Default="{StaticResource default}" ABCD="{StaticResource ABCD}"/> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn CellTemplate="{Binding TestField, Source={StaticResource vm}, Converter={StaticResource templateConverter}}"> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
我在绑定到自定义控件上的依赖属性时遇到问题。 这是我的自定义控件。它基本上是一个文本框,只允许数字输入并公开“值”依赖项
我想触发一个没有值的@Input setter,因此使用RxJS的Subject()。 然而,这只发出一次,显然是因为值没有改变。 我怎样才能让输入随时触发...
我正在尝试修改 XAML 中复选框的可见性。我已经这样做了如下: XAML 摘录: 我正在尝试修改 XAML 中 CheckBox 的可见性。我已经这样做了: XAML 摘录: <CheckBox Content="RES" IsChecked="{Binding Unit.Reserved}" Name="chk_Reserved" Visibility="{Binding Res_Visible}" ToolTip="Gereserveerd"> </CheckBox> ...但是,在调试时,如果我转到“实时可视化树”,我会在属性列表中看到以下内容: 如您所见,有一个针对 IsChecked 属性的绑定表达式,但没有针对 Visible 属性的绑定表达式,尽管这是 XAML 所期望的。 有人知道这是怎么可能的吗? (Visible属性不是“可绑定的”吗?) 在此,Res_Visible属性的定义: public class CassetteLevelViewModel:BaseViewModel { ... public Visibility Res_Visible { get; set; } ... Visibility 似乎是System.Windows.Visibility。它是否正确? (我以为还有一个UIElement.Visibility,还是我错了?) 我希望我正确理解了这个问题,您与 Res_Visible 属性进行了绑定,我怀疑您想更改可见性,但是当您从 CassetteLevelViewModel 更改它时,它不起作用。 要通知 View (Xaml) 您的情况下 ViewModel 中的属性 (CassetteLevelViewModel) 已更改值,您应该在 INotifyPropertyChanged 中使用 BaseViewModel。 如果您不使用,您可以在下面找到您应该添加的内容BaseViewModel。 public class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } OnPropertyChanged()方法通知View属性值已经改变。 如果您已经使用,您只需要修改您的属性,如下例所示。 private Visibility _res_Visibile; public Visibility Res_Visibile { get => _res_Visibile; set { _res_Visibile = value; OnPropertyChanged(); } } 经过相当多的实验,我在属性的定义中找到了解决方案: 我变了: public Visibility Res_Vis {get;set;} 进入: public Visibility Res_Vis {get;set;} = Visibility.Hidden; 现在一切正常。
绑定到 SwiftData @Query 数组中第一个元素的属性
我有一个 SwiftData 模型播放器,数据库中应该只有 0 或 1。当用户位于主菜单并点击“开始新游戏”时,我会引导他们通过几个屏幕来选择...
WPF:如何从 DatagridHeader 的 ContentTemplate 绑定到字典项
我正在尝试在数据网格标题上使用组合框实现过滤弹出窗口。我正在尝试将弹出窗口内组合框的项目源绑定到相关列的内容。 为了做到这一点,我
我正在尝试将命令绑定到毛伊岛地图上的标记单击事件。我使用 DataTemplate 绑定了引脚。我可以通过 MainPage.xaml.cs 让它工作,但我的应用程序正在使用 viewModel。我不能
我正在尝试使用 python Tkinter 编写一个 GUI 应用程序,我遇到了这个问题,我的主要问题是什么使以下代码中的按钮在关闭文件 exp 后保持按下状态(或按下)...
无法从 CollectionView 按钮点击 ICommand
在我使用 Mvvm.Toolkit 的应用程序中,我遇到了一个问题,即放置在 CollectionView 中的按钮在单击时无法调用 ViewModel 中关联的 ICommand 方法。
如何在 Angular 中定义值时仅在 HTML 元素中显示 id 属性
有这个简单的组件。有些用途需要输入字段上的 ID,有些则不需要。当值为假时,我不知道如何完全忽略 id 属性。这怎么可以……
正在寻找带有绑定的 findFragmentById 的替代方法?
有没有办法在不使用findFragmentById或findFragmentByTag的情况下获取对Fragment的引用?
“静态方法‘buildBlock’要求‘ToolbarItem<(), Button<some View>>’符合‘View’
静态方法“buildExpression”要求“ToolbarItem<(), Button>”符合“View” 公共结构SetAStatusView:查看{ @ObservedObject var 模型:StatusViewModel @环境(\.
我想在 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); }
将数据上下文字符串属性绑定到 StaticResource 键
我有一个带有 ResourceKey 和 Caption 的列表值,这些值都是字符串。 Resource是资源字典中定义的实际资源的名称。每个 ResourceKey Ico...
MAUI 将 FlyoutFooterTemplate 标签值设置为应用程序版本
我的 Shell 应用程序中的 FlyoutFooterTemplate DataTemplate 中有一个标签,我想将其设置为当前的应用程序版本 (AppInfo.Current.VersionString),但我很难尝试
我正在尝试从视图模型更新 xml 中的图像源,但似乎我无法做到这一点,而且我不明白我缺少什么。 所以...在视图 xml 中,我有一张图像,我为其制作了
我刚刚开始编码。 我发现自己陷入了可能非常简单的事情。 我正在做书上的作业 它几乎没有告诉我该怎么做,但它不起作用。 (
有没有办法在绑定 AAR 时删除对样式属性的引用?我的 AAR 在构建项目时会抛出错误,因为生成的 DLL 正在引用我的项目的样式属性...
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>