Windows Presentation Foundation或WPF是用于在基于Windows的应用程序中呈现用户界面的子系统。
使用RelativeSource绑定到ViewModel不起作用
我有一个应用程序,它有一个导航菜单,可以使用命令从一个页面转到另一个页面。 导航菜单已在 Xaml 中创建,我将其存储在 Navigation.xaml 中,请参见下文 <
我为一些自定义 WPF 控件构建了一系列事件处理程序。该事件处理用户根据包含的数据类型输入或离开文本框时显示的文本格式(电话
所以,我是 WPF 绘图新手。出于性能原因,我不得不从 ContentControl 和 UserControl 等常规控件切换到 DrawingVisual 等更轻量级的元素。我正在开发一个
我读过很多关于类似问题的帖子,但不知道如何摆脱右侧的额外空间。中间一列(应该是最右边的一列,带有“240”和...
我创建了一个装饰器,将其应用于文本框,这是为了我们定制的拼写检查。 每当有未知单词时,装饰器都会使用 Drawingcont 添加下划线...
我正在寻找使用尊重 MVVM ReactiveUi 原则的参数打开新窗口的最佳方法。现在我正在使用 Splat 的服务位置来打开新窗口。 假设我想打开新窗口...
我想知道是否有人知道复选标记符号的标签。我需要将它放在 texblock 中。我在网上寻找任何线索,但没有找到任何可以与 XAML 一起使用的东西。 预先感谢您。
我正在开发 Visual Studio 扩展,并尝试对 WPF DataGrid 进行主题化以匹配 Visual Studio 浅色或深色主题。除了左上角之外,我已经设法让一切正常工作......
是否可以创建如图所示的滚动条? 这张图片取自以下链接:http://codesdirectory.blogspot.be/2013/01/wpf-scrollviewer-control-style.html。 我尝试了这个例子...
我有一个带有 2 个文本框的小 WPF 窗口,已订购 TabIndex 0,1,并且我想在按 Enter 键时自动将焦点从第一个文本框移动到第二个文本框。 我使用 MVVM Light。 雷玛...
我在使用简单控件时遇到问题:加载窗口时,它会正确加载用户控件。当我最大化时,用户控件及其内容按预期大小增大。当我恢复过来时,...
绑定到对象 ObservableCollection 中对象的属性
我有一个主窗口,其中包含带有切换按钮的DataGrid,DataGrid的项目源是对象的ObservableCollection。我想遵循以下内容:当用户检查切换时,IsActive2 将墨水...
WPF .Net Framework 4.8 的窗口 Toast 通知显示为空
我按照以下说明安装了 Microsoft.Toolkit.Uwp.Notifications NuGet 包: https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=
尽管在编写 Winforms 应用程序方面有一些经验,但 WPF 在最佳实践和设计模式方面的“模糊性”仍然让我困惑。 尽管在运行时填充了我的列表,但我的
Dependency Property 和 DataContext 赋值的顺序
从我的测试来看,如果我仅通过 XAM 设置一次 DataContext 和依赖属性,则似乎总是先分配 DataContext,然后再分配依赖属性...
我正在编写一个 .net Framework 4.8.1 应用程序,该应用程序引用了来自几个不同第三方应用程序的大约 2 打 dll。这些 dll 又引用了来自...的更多(数千个)dll
我有控制 公共类 FocusTestControl :控制 { 公共 FocusTestControl() { DefaultStyleKey = typeof(FocusTestControl); } } 这是它的默认样式 <question vote="0"> <p>我有一个控制</p> <pre><code>public class FocusTestControl : Control { public FocusTestControl() { DefaultStyleKey = typeof(FocusTestControl); } } </code></pre> <p>这是它的默认样式</p> <pre><code> <Style TargetType="local:FocusTestControl"> <Setter Property="Focusable" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Grid> <Border Background="AliceBlue" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </code></pre> <p>我将此控件放在窗口上:</p> <pre><code><Window x:Class="MakeWpfControlFocusable.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MakeWpfControlFocusable" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="35" /> </Grid.RowDefinitions> <local:FocusTestControl /> <StackPanel Grid.Row="1" Orientation="Horizontal"> <TextBlock Text="Focused element: " /> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=KeyboardFocusedElement}" /> <TextBox Text="Text" /> </StackPanel> </Grid> </code></pre> <p></p> <p>但是单击控件并不会使其聚焦(我的意思是 KebordFocus)</p> <p>实际上,我的任务是处理 KeyDown 和 KeyUp 事件。但当元素没有键盘焦点时这是不可能的。</p> </question> <answer tick="true" vote="1"> <p>可以通过按 Tab 键盘键来设置键盘焦点。但点击 <pre><code>Control</code></pre> 时并未设置。我已订阅 <pre><code>MouseDown</code></pre> 事件并在处理程序中手动设置焦点。</p> <pre><code>public class FocusTestControl : Control, IInputElement { public FocusTestControl() { DefaultStyleKey = typeof(FocusTestControl); MouseDown += FocusTestControl_MouseDown; } void FocusTestControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { Keyboard.Focus(this); } } </code></pre> </answer> </body></html>
我有一个 WPF 应用程序,使用命令 dotnetpublish -r win-x64 /p:PublishSingleFile=true /p:InincludeNativeLibrariesForSelfExtract=true --self-contained true --output ... 部署到一个 exe 中
WPF 以编程方式将数据网格组合框列绑定到 itemsource
在我的数据网格中,其中一列是 DataGridComboBoxColumn,我试图在每一行中显示不同的下拉菜单。在 XAML 中创建组合框而不是通过编程来创建组合框是一项简单的任务...
我想将字符串绑定格式化为 Amount is X,其中 X 是绑定到标签的属性。 我见过很多例子,但以下不起作用: 我想将字符串绑定格式设置为 Amount is X,其中 X 是绑定到标签的属性。 我见过很多例子,但以下不起作用: <Label Content="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" /> 我也尝试过这些组合: StringFormat=Amount is {0} StringFormat='Amount is {}{0}' StringFormat='Amount is \{0\}' 我什至尝试将绑定属性的数据类型更改为 int、string和 double。似乎什么都不起作用。这是一个非常常见的用例,但似乎不受支持。 这不起作用的原因是 Label.Content 属性的类型为 Object,并且 Binding.StringFormat 仅在绑定到 String 类型的属性时使用。 发生的事情是: Binding 正在对您的 MaxLevelOfInvestment 值进行装箱并将其存储为 Label.Content 属性作为装箱的十进制值。 标签控件有一个包含 ContentPresenter 的模板。 由于未设置 ContentTemplate,因此 ContentPresenter 会查找为 DataTemplate 类型定义的 Decimal。当它找不到时,它会使用默认模板。 ContentPresenter 使用的默认模板通过使用标签的 ContentStringFormat 属性来呈现字符串。 有两种可能的解决方案: 使用 Label.ContentStringFormat 而不是 Binding.StringFormat,或者 使用 String 属性(例如 TextBlock.Text)而不是 Label.Content 这里是如何使用Label.ContentStringFormat: <Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}" /> 以下是如何使用 TextBlock: <TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" /> 注意:为了简单起见,我在上面的解释中省略了一个细节:ContentPresenter实际上使用了自己的Template和StringFormat属性,但在加载过程中,它们会自动模板绑定到ContentTemplate和ContentStringFormat属性Label,所以看起来 ContentPresenter 实际上正在使用 Label 的属性。 制作一个通用的StringFormatConverter : IValueConverter。将格式字符串传递为 ConverterParameter. Label Content="{Binding Amount, Converter={...myConverter}, ConverterParameter='Amount is {0}'" 此外,当格式字符串中需要多个对象时,请使用 StringFormatMultiConverter : IMultiValueConverter,例如 Completed {0} tasks out of {1}。 我刚刚检查过,由于某种原因它不适用于 Label,可能是因为它在内部使用 ContentPresenter 来表示 Content 属性。您可以使用 TextBlock 来代替,这样就可以了。如果您需要继承样式、行为等,您也可以将下面的 TextBlock 摘录放在 Label 的内容中。 <TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} /> 尝试使用转换器...... <myconverters:MyConverter x:Key="MyConverter"/> <Label Content="{Binding Path=MaxLevelofInvestment, Converter={StaticResource MyConverter"} /> public class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return String.Format("Amount is {0}", value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } }