wpf 相关问题

Windows Presentation Foundation或WPF是用于在基于Windows的应用程序中呈现用户界面的子系统。

从代码隐藏中调用命令

所以我一直在四处寻找,但找不到确切的方法。我正在使用 MVVM 创建用户控件,并希望在“Loaded”事件上运行命令。我意识到这需要我...

回答 7 投票 0

C# WPF:按下时更改按钮 Context="{Binding Text}"

WPF 编程新手我来自 WinForms。我有两个正在运行的 WinForms 项目/解决方案,我想将它们转换为 WPF。我设法通过 XMAL 和 MainWindowViewModel 但...

回答 1 投票 0

如何在 WPF C# 中处理时保持 UI 畅通

我已经阅读了很多有关线程和异步的示例...,但是我无法使 wpf c# 中的 UI 在计算磁盘上的文件时不冻结 - 我无法移动窗口...,我有等待计数

回答 1 投票 0

在Wpf中,如何在运行时修改DataTemplate?

我正在编写一个插件,该插件已加载到源不受我管理的应用程序中。通过 API,我可以获得应用程序的 AvalonDock DockingManager。我想添加一个绑定...

回答 1 投票 0

如何正确管理BitmapImage的数据绑定和序列化/反序列化?

这是我的基本代码: 公共类 MyObject { 公共十进制 MyDeciaml { 获取;放; } 公共字符串?我的ImageBase64 { 得到 { 如果(MyImageBase64!=空) ...

回答 1 投票 0

在按钮命令上,将 UserControl 上的 ItemControl 项传递到 ViewModel

我想是时候寻求帮助了!我有一个列出 UserControls 的 ItemsControl(称为CompoundCardControl): 我想是时候寻求帮助了!我有一个列出 UserControls 的 ItemsControl(称为CompoundCardControl): <ScrollViewer Grid.Row = "1" VerticalScrollBarVisibility="Auto"> <ItemsControl ItemsSource="{Binding Method.Compounds}"> <ItemsControl.ItemTemplate> <DataTemplate> <control:CompoundCardControl/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> UserControl 包含一个按钮,用于从 ListView(和 ViewModel)中删除项目: <Grid Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="25"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Margin="5,0,0,0" Text="{Binding Name}" /> <TextBlock Grid.Column="1" Margin="5,0,0,0" Foreground="Gray" FontSize="10" VerticalAlignment="Center" Text="RT"/> <TextBlock Grid.Column="2" Margin="5,0,0,0" Text="{Binding RetentionTime , StringFormat= '\{0\} minutes'}" /> <Button Grid.Column="3" HorizontalAlignment="Right" Style="{StaticResource menuBarButton}" Command="{Binding DataContext.DeleteCompoundCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding Compound}"> <Image Width="12" Height="12" Source="/Resources/Icons/Delete.png"/> </Button> </Grid> 这是我的父窗口的 ViewModel(但它显然不起作用)”: public class MethodDialogViewModel : ViewModelBase { //properties public DialogModeEnum DialogMode { get; private set; } public string WindowTitle { get { return DialogMode == DialogModeEnum.Add ? "New method" : "Edit method"; } } public string ButtonText { get { return DialogMode == DialogModeEnum.Add ? "Add" : "Update"; } } public Method Method { get; set; } public MethodCompound Compound { get; set; } = new MethodCompound(); //constructor public MethodDialogViewModel(Method method, DialogModeEnum dialogMode) { DialogMode = dialogMode; Method = method; } //commands public RelayCommand DeleteCompoundCommand => new(execute => DeleteCompound(Compound)); private void DeleteCompound(MethodCompound methodCompound) { //Logic not implimented. I just want to see if I can get the object "methodCompound" if (methodCompound != null) { MessageBox.Show(methodCompound.ID); } else { MessageBox.Show("Please select a compound.", "Delete compound", MessageBoxButton.OK, MessageBoxImage.Information); } } } 基本上我想将 ItemsControl 中的 Item(实际上是底层对象)传递给 ViewModel。我怎么做?还是我完全搞砸了? 您的代码存在一些奇怪之处,这似乎导致了为什么没有任何内容按预期工作。 RelayCommand 委托似乎是错误的。根据参数名称execute,您似乎得到了错误的委托签名。 RelayCommand实现的原始签名是ICommand.Execute(object commandParameter): void。这意味着 lambda 的 execute 参数应命名为,例如commandParameter。然后您实际上必须将该参数传递给执行委托。否则,Button.CommandParameter值将丢失,即不转发: 公共 RelayCommand 删除复合命令 => new(commandParameter => DeleteCompound((MethodCompound )commandParameter)); MethodDialogViewModel.Compound属性始终返回相同的实例 Grid.Width内CompoundCardControl上的绑定是多余的,因为Grid会自然拉伸以填充可用空间。 因为您想要滚动 ItemsControl 内部的项目,而不是 ItemsControl 本身,所以必须将 ScrollViewer 添加到 ControlTemplate 的 ItemsControl 中,它包裹着 ItemsPresenter。 <ControlTemplate TargetType="ItemsControl"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <ScrollViewer> <ItemsPresenter /> </ScrollViewer> </Border> </ControlTemplate> 请注意,ItemsControl不提供任何性能功能(例如 UI 虚拟化),这可以显着改善较大列表或包含昂贵项目或项目容器的列表的体验。 另一方面,ListBox 是一种高级的 ItemsControl,带有滚动和 具有 UI 虚拟化和一些有用的属性,例如返回当前所选项目的 ListBox.SelectedItem 属性。在您的情况下,您可以将其绑定到 MethodDialogViewModel.Compound 属性。要消除突出显示,您只需覆盖 ListBoxItem.Template: <!-- A ListBoxItem without highlighting. You can add this template to a resource to reuse it. --> <ControlTemplate x:Key="NoHighlightListBoxItemTemplate" TargetType="ListBoxItem"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <ContentPresenter /> </Border> </ControlTemplate> 一些一般性说明:自定义控件(例如 UserControl)永远不应该使用自己的 DataContext。例如,当 DataContext 发生变化时,控件就会损坏。由于 Binding.RelativeSource 的目标元素是自定义控件之外的,因此将自定义控件移动到可视化树中的其他位置也会破坏自定义控件。为了防止这种情况,请添加请求将所需数据传递到自定义控件的依赖属性,例如通过数据绑定。然后将内部控件绑定到这些依赖属性。目前尚不清楚为什么在这种情况下使用 UserControl。您可以通过直接在 UserControl 中定义项目的布局来安全地避免 DataTemplate 的开销。如果需要分隔,您可以将 DataTemplate 移动到其自己的文件中。 这与解决您的问题无关,但如果您关心良好的控制设计,则与此相关。这些是一些基础知识。 CompoundCardControl.xaml.cs public partial class CompoundCardControl : UserControl { public MethodCompound Compound { get => (MethodCompound)GetValue(CompoundProperty); set => SetValue(CompoundProperty, value); } public static readonly DependencyProperty CompoundProperty = DependencyProperty.Register( "Compound", typeof(MethodCompound), typeof(CompoundCardControl), new PropertyMetadata(default)); public ICommand DeleteCommand { get => (ICommand)GetValue(DeleteCommandProperty); set => SetValue(DeleteCommandProperty, value); } public static readonly DependencyProperty DeleteCommandProperty = DependencyProperty.Register( "DeleteCommand", typeof(ICommand), typeof(CompoundCardControl), new PropertyMetadata(default)); public CompoundCardControl() { InitializeComponent(); } } CompoundCardControl.xaml <UserControl x:Name="Root"> <Button Style="{StaticResource menuBarButton}" Command="{Binding ElementName=Root, Path=DeleteCommand}" CommandParameter="{Binding ElementName=Root, Path=Compound}"> <Image Source="/Resources/Icons/Delete.png"/> </Button> </UserControl> MainWindow.xaml <Window> <Window.DataContext> <MethodDialogViewModel /> </Window.DataContext> <Window.Resources> <!-- A ListBoxItem without highlighting --> <ControlTemplate x:Key="NoHighlightListBoxItemTemplate" TargetType="ListBoxItem"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ContentPresenter /> </Border> </ControlTemplate> </Window.Resources> <ListBox ItemsSource={Binding Method.Compounds}"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Template" Value="{StaticResource NoHighlightListBoxItemTemplate}" /> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate DataType="{x:Type MethodCompound}"> <control:CompoundCardControl Command="{Biding DeleteCompoundCommand}" Compound="{Binding}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Window> MethodDialogViewModel.cs public class MethodDialogViewModel : ViewModelBase { //properties public DialogModeEnum DialogMode { get; private set; } public string WindowTitle { get { return DialogMode == DialogModeEnum.Add ? "New method" : "Edit method"; } } public string ButtonText { get { return DialogMode == DialogModeEnum.Add ? "Add" : "Update"; } } public Method Method { get; set; } // This does never change. Consider to bind it to the ListBox.SelectedItem property public MethodCompound Compound { get; set; } = new MethodCompound(); //constructor public MethodDialogViewModel(Method method, DialogModeEnum dialogMode) { DialogMode = dialogMode; Method = method; } //commands public RelayCommand DeleteCompoundCommand => new(ExecuteDeleteCompoundCommand); private void ExecuteDeleteCompoundCommand(object commandParameter) => DeleteCompound((MethodCompound)commandParameter); private void DeleteCompound(MethodCompound methodCompound) { // methodCompund will never be NULL in the current scenario this.Method.Compunds.Remove(methodCompound); } }

回答 1 投票 0

如何获取给定属性绑定到 WPF C# 的所有 UI 控件

我正在使用反射来获取属性列表,并且我想使用该属性来获取给定属性所绑定到的 WPF 控件的实例。 所以在后面的代码中我想...

回答 1 投票 0

wpf:为 Cefsharp 浏览器设置命令行设置

我的浏览器有以下 xaml 代码: 我想为浏览器设置命令行参数,例如

回答 1 投票 0

动态添加列表框项目时出现问题

我正在尝试向 ModelB 上的 ListBox 添加一条来自 ModelA 的消息 1.- 每当 ModelA 上发生触发时, A型 公共字符串消息 { 获取=>_消息; 放 {

回答 1 投票 0

缺少一些装配参考

所以我遇到了一个问题,我的项目突然出现一个错误,内容与我所做的标题相同。我以前从未遇到过这个。当我以常规模式运行 Visual Studio 2020 时,就会发生这种情况,...

回答 1 投票 0

列系列 x 轴不同间隔的不同颜色填充同一个系列?

我正在尝试实现速度/时间绘图 UI,我使用带有 MVVM 模式的 WPF 和 Beto-rodriguez 的实时图表作为我的绘图库。 我正在使用列系列。 我有两个问题: 1)我必须...

回答 4 投票 0

Spotify API 客户端获取播放列表曲目偏移量

我是 API 的新手,想尝试一下 Spotify API。 我使用 JohnnyCrazy 的 Spotify API 客户端,到目前为止它运行得很好。 使用令牌进行身份验证适用于...

回答 2 投票 0

从 INotifyPropertyChanged 切换到 ObservableObject

下午好,我正在 wpf、.net 8.0 框架、mvvm 社区工具包中进行运行时全球化。我设法在没有工具包的情况下做到了这一点(INotifyPropertyChanged,#commented code),但我不能...

回答 1 投票 0

WPF 应用程序运行并行操作

我正在开发一个小型软件应用程序。其工作原理的总体逻辑是这样的。 UDP 字符串正在本地网络上广播 应用程序将在

回答 1 投票 0

在 C# Wpf 应用程序中,如何动态更改用户控件中的按钮图标

所以在我的应用程序中我有一个用户控件。除其他外,它还有一个带有定义为几何数据的图像的按钮: 所以在我的应用程序中我有一个用户控件。除其他外,它还有一个按钮,其中的图像定义为几何数据: <!-- Add new Item Button --> <Button x:Name="createButton" Width="40" Height="40" Margin="0,0,20,0" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding OpenNewWindowCommand}" Style="{StaticResource RoundAccentButton}"> <!-- Icon --> <Viewbox x:Name="createViewbox" Width="22" Height="22"> <Path Data="{StaticResource NewTaskIcon}" Fill="{StaticResource OnAccent}" /> </Viewbox> </Button> ..放置在 ResourceDictionary 中的几何数据如下所示: <!-- New Task Icon --> <PathGeometry x:Key="NewTaskIcon" Figures="M17 21.2188V17.4688H13.25V14.9688H17V11.2188H19.5V14.9688H23.25V17.4688H19.5V21.2188H17ZM0.75 17.5V15H3.25V17.5H0.75ZM5.75 17.5V15H10.8438C10.7812 15.4375 10.7552 15.8542 10.7656 16.25C10.776 16.6458 10.8125 17.0625 10.875 17.5H5.75ZM0.75 12.5V10H3.25V12.5H0.75ZM5.75 12.5V10H14.0625C13.5833 10.3333 13.151 10.7083 12.7656 11.125C12.3802 11.5417 12.0417 12 11.75 12.5H5.75ZM0.75 7.5V5H3.25V7.5H0.75ZM5.75 7.5V5H20.75V7.5H5.75ZM0.75 2.5V0H3.25V2.5H0.75ZM5.75 2.5V0H20.75V2.5H5.75Z" /> 问题是我当然不想将 PathData 设置为用户控件中的硬编码值,而是将其设置在使用用户控件的位置。例如,我在另一个名为 TaskView.xaml 的视图中使用这个名为 SearchItems 的用户控件,如下所示: <UserControl x:Class="myTaskManager.UI.Wpf.MVVM.Views.TaskView" 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:uc="clr-namespace:myTaskManager.UI.Wpf.MVVM.UserControls" xmlns:vm="clr-namespace:myTaskManager.UI.Wpf.MVVM.ViewModels" d:DesignHeight="450" d:DesignWidth="435" mc:Ignorable="d"> <UserControl.DataContext> <vm:TaskViewViewModel /> </UserControl.DataContext> <DockPanel> <Border Width="435" Background="{StaticResource TaskListBackground}"> <uc:SearchItems /> </Border> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="140" /> </Grid.ColumnDefinitions> <!-- Task Viewer --> <Grid Grid.Column="0"> <Grid.RowDefinitions> 。 。 . 我想做的是使用 PathData 访问 Viewbox 以根据用户控件的使用位置更改图标。我想我可以通过定义一个 DependencyProperty 来做到这一点,假设我们将其称为 PathData,然后将其设置在我使用用户控件的视图中,即类似这样的内容(下面是定义为用户控件的视图,它使用 SearchItems 用户控件) : <UserControl x:Class="myTaskManager.UI.Wpf.MVVM.Views.TaskView" 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:uc="clr-namespace:myTaskManager.UI.Wpf.MVVM.UserControls" xmlns:vm="clr-namespace:myTaskManager.UI.Wpf.MVVM.ViewModels" d:DesignHeight="450" d:DesignWidth="435" mc:Ignorable="d"> <UserControl.DataContext> <vm:TaskViewViewModel /> </UserControl.DataContext> <DockPanel> <Border Width="435" Background="{StaticResource TaskListBackground}"> <uc:SearchItems PathData="{StaticResource NewTaskIcon}" /> </Border> ..或者简单地在用户控件的代码隐藏中定义一个名为 PathData 的属性: public Geometry PathData { get; set; } 然后在用户控件的xaml代码中这样做: <!-- Add new Item Button --> <Button x:Name="createButton" Width="40" Height="40" Margin="0,0,20,0" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding OpenNewWindowCommand}" Style="{StaticResource RoundAccentButton}"> <!-- Icon --> <Viewbox x:Name="createViewbox" Width="22" Height="22"> <Path Data="{Binding PathData}" Fill="{StaticResource OnAccent}" /> </Viewbox> </Button> ..然后在使用用户控件的视图中只需使用与上面相同的语法,即: <UserControl x:Class="myTaskManager.UI.Wpf.MVVM.Views.TaskView" 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:uc="clr-namespace:myTaskManager.UI.Wpf.MVVM.UserControls" xmlns:vm="clr-namespace:myTaskManager.UI.Wpf.MVVM.ViewModels" d:DesignHeight="450" d:DesignWidth="435" mc:Ignorable="d"> <UserControl.DataContext> <vm:TaskViewViewModel /> </UserControl.DataContext> <DockPanel> <Border Width="435" Background="{StaticResource TaskListBackground}"> <uc:SearchItems PathData="{StaticResource NewTaskIcon}" /> </Border> ..我动态设置 PathData,但这些方法似乎都不起作用。实现这一目标的最佳方法是什么? 任何为我指明正确方向的答案将不胜感激。 谢谢, 彼得 UserControl 应公开一个 依赖属性 public static readonly DependencyProperty IconDataProperty = DependencyProperty.Register( nameof(IconData), typeof(Geometry), typeof(TaskView)); public Geometry IconData { get => (Geometry)GetValue(IconDataProperty); set => SetValue(IconDataProperty, value); } 通过 RelativeSource 绑定在控件的 XAML 中的 Path 元素中使用。请注意,不需要 Viewbox。 <Button Width="40" Height="40" ...> <Path Width="22" Height="22" Data="{Binding IconData, RelativeSource={RelativeSource AncestorType=UserControl}}" Fill="{StaticResource OnAccent}" Stretch="Uniform"/> </Button>

回答 1 投票 0

如何从文件夹wpf获取文件路径?

我遇到了一个问题,我正在开发一个 WPF 应用程序,其中有一个名为“帮助”的页面,其中有一个按钮 当用户单击此按钮时,我必须向用户提供 pdf 文件,这是我...

回答 9 投票 0

Fluxor 与 WPF

我有一个旧版 WPF 应用程序,已转换为 .NET 8。一切都运行良好,除了应用程序中的单例,它无法轻松传输到 Blazor 服务器应用程序。我本来可以...

回答 1 投票 0

WPF:命令被禁用

我有一个 WPF UserControl,其中包含一个带有 5 个按钮的工具栏。其中 4 个按钮的命令在父窗口中处理,但第五个按钮的命令由 UC 本身处理。它...

回答 2 投票 0

ResizeMode =“NoResize”和WindowStyle =“None”的WPF窗口不会掉落阴影

我在使用 WPF 中的窗口时遇到问题 如果我设置: 调整大小模式=“不调整大小” 窗口样式=“无” 窗户看起来是平坦的,没有圆形边框和下方的阴影。 是不是...

回答 1 投票 0

WPF C# GroupBox 控件

我目前需要使用组框获取文本框中的内容信息,这些文本框具有相同的前五个字符。我对 WPF C# 还很陌生,并且阅读了一些关于...的文章。

回答 1 投票 0

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