wpf 相关问题

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

Wpf 子表单,OnClosing 事件和等待

我有一个从父表单启动的子表单,其中包含: ConfigForm cfg = new ConfigForm(); cfg.ShowDialog(); 该子表单用于配置一些应用程序参数。 我想检查一下是否...

回答 2 投票 0

WPF PreviewMouseRightButtonDown 与 MouseRightButtonDown 事件

当我们在处理鼠标右键事件时应该使用 PreviewMouseRightButtonDown 事件而不是 MouseRightButtonDown 事件? 请详细说明易用性。

回答 2 投票 0

从自定义 UserControl 的 DependencyProperty 返回值并使用 viewModel 的正确方法?

我将自定义用户控件包装到 dll,并希望引用它的其他人能够通过绑定到“Result”属性来获取或设置结果值。 这是 CustomControl.cs 代码: 公开部分

回答 1 投票 0

RadListBox 上的 ScrollView (ScrollInfo) Action 导致内存泄漏

[在此输入图像描述][1]我在RadListBox中添加一个项目并始终更新它以实时记录程序的运行状态 将项目添加到列表框不是问题,但我...

wpf
回答 1 投票 0

向超链接 C# 添加导航

我想在点击某个超链接时添加一个导航到另一个页面的过程。 我想在点击某个超链接时添加到另一个页面的导航过程。 <DataGridHyperlinkColumn x:Name="Hyperlink" Header="Hyperlink" ContentBinding="{Binding Name}" DataGridHyperLinkColumn 没有点击功能。那么如何实现“点击功能”呢? 预先感谢 在您的代码隐藏中,您可以尝试订阅超链接的 RequestNavigate 事件。 // In constructor or after component initialization. Hyperlink.RequestNavigate += Hyperlink_RequestNavigate; 在 Hyperlink_RequestNavigate 方法中,您可以处理导航逻辑。 private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) { // Navigate to the URL specified in the hyperlink Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); e.Handled = true; }

回答 1 投票 0

WPF 每个 TabItem 的初始焦点

好吧,我知道这个标题看起来到处都有答案,但这里找到的答案都不适合我。那么,就这样吧。 我有这样的布局: 好吧,我知道这个标题看起来到处都有答案,但这里找到的答案都不适合我。那么,就到这里吧。 我有这个布局: <Window> <Grid> <DockPanel> <TabControl> <TabItem> <Page x:Name="p"> <Grid x:Name="g2"> <TabControl x:Name="tc"> <TabItem x:Name="ti1"> <StackPanel x:Name="sp"> <C:TextBox x:Name="txt"/> </StackPanel> </TabItem> <TabItem x:Name="ti2"> <C:DataGrid x:Name="dg"/> </TabItem> </TabControl> </Grid> </Page> </TabItem> </TabControl> </DockPanel> </Grid> </Window> 现在,我的目标是当选择 txt TextBox 时将焦点放在 ti1 TabItem 上,并在选择 dg DataGrid 时将焦点放在 ti2 TabItem 上。另外,我真的很想将其设置为XAML。 注意:我只能使用此处命名的控件,所以直到 Page 控件。 到目前为止我尝试过什么: 在 FocusManager.FocusedElement="{Binding ElementName=txt}" txt 的父树中的所有父控件上设置 Control(直到 Page)。 在 FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" 和 txt 控件上设置 dg。 通过 TabControl 的 SelectionChanged 事件在代码中设置焦点: if ( ti1.IsSelected ) { tc.UpdateLayout(); FocusManager.SetFocusedElement( sp, txt ); } 和 if ( ti1.IsSelected ) { tc.UpdateLayout(); txt.焦点(); } TextBox和DataGrid控件的创建方式与UserControl类似,但实际上是继承TextBox和DataGrid的类,如下所示: <TextBox ... </TextBox> 和 public partial class TextBox : System.Windows.Controls.TextBox 正如我所说,XAML解决方案是理想的,但如果前者不可能的话,我也会选择代码一。 好吧,Keyur PATEL 的答案中的调度程序部分对我来说是解决方案,尽管不是完整的解决方案。我的答案是用 TabControl 更新 Dispatcher 布局,然后调用 Focus Dispatcher。所以,对我来说完整的答案是: Dispatcher.BeginInvoke( (Action) (() => tc.UpdateLayout()) ); Dispatcher.BeginInvoke( (Action) (() => txt.Focus() ) ); 或者您可以只使用 Invoke 来代替,让 UI 线程等待您的 Action。 之所以我必须使用Dispatcher,是因为我首先用它来更改选定的选项卡。至少这是我最好的猜测。 您可以尝试类似于代码隐藏解决方案的方法,但有以下变体: <TabControl x:Name="tc" SelectionChanged="tc_selectionChanged"> 并在后面的代码中: InitializeComponent(); //if you know which control to focus by default when page is first loaded Dispatcher.BeginInvoke(new Action(() => { txt.Focus(); })); 和 private void tc_selectionChanged(object sender, SelectionChangedEventArgs e) { if (ti1.IsSelected) { txt.Focus(); } else if (ti2.IsSelected) { dg.Focus(); } } 我在自己的 WPF 应用程序上尝试了这个确切的设置,所以我知道它有效。 有用的链接: WPF TabControl 在 SelectionChanged 上,将焦点设置到文本字段 和 如何在 WPF 中的 tabItem 中集中控制(尽管对我来说,它不需要 UpdateLayout()) 找到使用扩展属性的解决方案 public static class TabItemExtention { public static bool GetIsSelected(DependencyObject obj) { return (bool)obj.GetValue(IsSelectedProperty); } public static void SetIsSelected(DependencyObject obj, bool value) { obj.SetValue(IsSelectedProperty, value); } public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached( "IsSelected", typeof(bool), typeof(TabItemExtention), new UIPropertyMetadata(false, OnIsSelectedPropertyChanged)); private static void OnIsSelectedPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var uie = (UIElement)d; var tabItem = uie as TabItem; if (tabItem != null) tabItem.IsSelected = (bool)e.NewValue; if ((bool)e.NewValue) { uie.UpdateLayout(); } } } XAML: ctrl:TabItemExtention.IsSelected="{Binding IsTabNewCustomsDelaySelected}" 对我有用的是扩展方法。有些东西阻止我的元素获得焦点,所以等到上下文空闲修复了这个问题。 public static class UIElementExtensions { public static void FocusOnIdle(this UIElement element) { var result = element.Focus(); if (result == false) { Application.Current.Dispatcher.Invoke(new Action(() => { element.Focus(); }), Threading.DispatcherPriority.ContextIdle); } } }

回答 4 投票 0

在 UI 线程上创建并启动任务

当在工作线程上调用的方法需要在 UI 线程上运行代码并等待其完成后再执行其他操作时,可以这样做: 公共 int RunOnUi(Func&...

回答 1 投票 0

如何为给定的字符串生成“随机常量颜色”?

如何在运行时为给定字符串生成“随机常量颜色”? 因此,给定的字符串值将始终具有相同的颜色,但不同的字符串将具有不同的颜色。 就像如何...

回答 4 投票 0

从外部文件在自己的运行空间中显示 wpf 对话框

我有这个概念验证的 powershell 脚本,它将以其当前的形式工作,并将显示一个空白对话框,但以 wpf-ui 库为主题。 如果我更改 $reader = (New-Object System.Xml.XmlNode...

回答 1 投票 0

允许用户将大小调整为负高度/宽度

我正在 WPF 中创建一个类似绘画的工具,并遇到了以下问题,当用户单击绘制的形状(DrawingShape)时,将出现一个带有 8 个拇指的装饰器(矩形的每一侧各一个......

回答 1 投票 0

wpf组合框如何处理PreviewMouseWheel

我想将 MouseWheel 的处理程序添加到 ComboBox,但我不知道如何操作。 原因是我在 ScrollViewer 内的 DataGrid 内的 ComboBox 上遇到滚动问题,请参阅此处,我...

回答 1 投票 0

InvalidOperationException:调度程序处理已暂停,但消息仍在处理中

我们遇到了与此异常有关的多个问题,但我找不到有关问题的真正原因、此错误的所有可能来源以及我们应该避免什么的技术文档...

回答 2 投票 0

MVVM - 当绑定属性不存在时隐藏控件

我想知道如果视图模型中不存在控件绑定的属性,是否可以隐藏视图上的控件。例如,如果我有以下内容: 我想知道如果视图模型中不存在控件绑定的属性,是否可以隐藏视图上的控件。例如,如果我有以下内容: <CheckBox Content="Quote" IsChecked="{Binding Path=IsQuoted}" /> 我可以在 XAML 中检测到视图模型上不存在 IsQuoted 属性,并简单地隐藏该实例中的控件吗? 我本质上是创建一个向导对话框,该对话框在视图模型集合中移动,显示每个视图模型的关联视图。对于集合中的某些视图模型,将存在“IsQuoted”属性,而对于某些视图模型则不存在。 我希望在这些视图之外有一个复选框,当当前视图模型具有该属性时显示,当视图模型不具有该属性时隐藏。所有视图模型都派生自一个公共基类,但我不想通过添加“ShowQuoted”属性等来弄乱基类。 想法?并且,预先感谢... 使用始终返回 Visibility.Visible 的转换器来处理该值存在的情况。通过指定回退值来处理该值不存在的情况。当该属性不存在时,绑定失败并接收后备值。 <Page.DataContext> <Samples:OptionalPropertyViewModel/> </Page.DataContext> <Grid> <Grid.Resources> <Samples:AlwaysVisibleConverter x:Key="AlwaysVisibleConverter" /> </Grid.Resources> <CheckBox Content="Is quoted" IsChecked="{Binding IsQuoted}" Visibility="{Binding IsQuoted, Converter={StaticResource AlwaysVisibleConverter}, FallbackValue=Collapsed}" /> </Grid> public class OptionalPropertyViewModel { public bool IsQuoted { get; set; } } public class AlwaysVisibleConverter : IValueConverter { #region Implementation of IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion } 我修改了Phil的答案以使TargetNullValue起作用: public class AlwaysVisibleConverter : IValueConverter { public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture) { if (value is null) { // Makes TargetNullValue work. // Apparently TargetNullValue doesn't look at the target directly, but through this converter. return null; } return Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 并且: <CheckBox Content="Is quoted" IsChecked="{Binding IsQuoted}" Visibility="{Binding IsQuoted, Converter={StaticResource AlwaysVisibleConverter}, FallbackValue=Collapsed TargetNullValue=Collapsed}" />

回答 2 投票 0

取消任务后代码未执行

我们将异步命令的执行绑定到导致任务的 ui。另一个命令取消了此任务,但它无法正常工作。 公共类MainWindowViewModel:BaseViewModel {

回答 1 投票 0

WPF 双向绑定会话错误被隐藏

我想将 DataRowView 绑定到窗口上的控件。 初始化代码: ... 行=查询.DefaultView[0]; 数据上下文=行; XAML: ...

回答 1 投票 0

克隆自定义 UserControl C# WPF - 最简单的方法(浅复制)

我想克隆(浅克隆)自定义用户控件,例如: uc按钮 uc; uc按钮 uc2; uc = uc2; 当我这样做时,编译器希望我从画布中删除旧实例(uc)。 使用: Canvas1.Chi...

回答 1 投票 0

在同一时间轴上对两个对象进行动画处理。相继。 WPF

如何在同一时间线但以不同的时间间隔对两个对象的不透明度进行动画处理? 我期待这两个文本元素逐渐淡入,从一个文本框到另一个文本框。相反,Th...

回答 1 投票 0

WPF 在关闭事件中隐藏窗口可防止应用程序终止

又是一个简单的问题。 我使用 WPF 中的一个窗口作为子窗口,我宁愿让“X”按钮隐藏窗口而不是关闭窗口。为此,我有: 私人无效Window_Closing(o...

回答 4 投票 0

获得焦点和 KeyEventArgs

private void TextBox27_GotFocus(对象发送者,RoatedEventArgs e) { Stil53.Begin(); TextBox27.Select(TextBox27.Text.Length, 0); Takvim_Kapat();

wpf
回答 1 投票 0

调整 DataGrid 大小时出现意外的垂直线

我正在使用以下代码使用 DataGrid: 我正在使用以下代码使用 DataGrid: <DataGrid Name="DataGridBids" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HeadersVisibility="Column" Background="#0d4338" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" ItemsSource="{Binding Bids}" AutoGenerateColumns="False" IsReadOnly="True" IsManipulationEnabled="False" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" GridLinesVisibility="None" Margin="0,0,0,0" BorderThickness="0"> <DataGrid.Resources> <Style TargetType="DataGridCell"> <Setter Property="TextBlock.TextAlignment" Value="Center"/> <Setter Property="Background" Value="#0d4338"/> <Setter Property="Foreground" Value="#11b376"/> <Setter Property="BorderThickness" Value="0,0,0,0"/> </Style> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="Agent" Width="*" Binding="{Binding Agent}"/> <DataGridTextColumn Header="Quantity" Width="*" Binding="{Binding Quantity}"/> <DataGridTextColumn Header="Buy" Width="*" Binding="{Binding Price, StringFormat='N2'}"/> </DataGrid.Columns> </DataGrid> 当我打开 DataGrid 时,它的工作方式与我预期的完全一样: 但是当我调整窗口大小时,这些意想不到的白色垂直线不断出现(尽管并非总是如此): 由于某种原因,白线仅显示在第二列和第三列之间,并且始终垂直。 我不知道这是否是 WPF 错误,但无论如何我都想修复它。 任何建议都值得赞赏,谢谢! 渲染一定有什么奇怪的地方。由于您的列都打算具有相同的背景,因此您的样式可以像这样以 DataGridRow 而不是 DataGridCell 为目标(注意:我使用透明,因为 DataGrid 已经具有您想要的背景颜色): <DataGrid.Resources> <Style TargetType="DataGridRow"> <Setter Property="Background" Value="Transparent" /> <Setter Property="TextBlock.TextAlignment" Value="Center" /> <Setter Property="Foreground" Value="#11b376" /> <Setter Property="BorderThickness" Value="0,0,0,0" /> </Style> </DataGrid.Resources> 这消除了我在调整网格大小时看到的任何奇怪的锯齿,但我无法准确重现您所显示的内容。

回答 1 投票 0

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