xaml 相关问题

可扩展应用程序标记语言(XAML)是一种基于XML的声明式语言,用于在各种框架中初始化结构化值和对象。当问题是关于具有特定框架的XAML的使用时,还应该提供框架的标签,例如, [wpf](Windows Presentation Foundation),[silverlight],[windows-phone],[windows-store-apps](Windows 8商店应用),[win-universal-app],[xamarin.forms]或[工作流程 - 基础]

ComboBox SelectedValuePath 验证不起作用

我第一次尝试在 XAML / WPF 中实现数据验证。我读过一些教程,似乎有很多种方法可以做到这一点。我已经决定了一种方法,它......

回答 2 投票 0

带有图像和文本的WPF按钮(整个按钮可点击)

我有一个带有 Devexpress 的 XAML 代码的按钮,如下所示: 我有一个带有 Devexpress 的 XAML 代码的按钮,如下所示: <dxlc:LayoutItem Width="80" Margin="0,0,0,0"> <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ob:ViewModel}}, Path=DataContext.ClickCommand}" ToolTip="CLick this button" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="1" BorderBrush="DimGray"> <StackPanel Orientation="Horizontal"> <Image Source="../../../Resources/Ext/image.png" MaxHeight="16" MinHeight="16" Stretch="Uniform" /> <dxe:TextEdit EditValue="Click Me" IsReadOnly="True" ShowBorder="False" Foreground="White" Background="Transparent"/> </StackPanel> </Button> </dxlc:LayoutItem> 使用此代码,只有按钮的边框可单击,而文本区域则不可单击。如何在按钮旁边放置图像并让按钮的整个区域都可单击? WPF 似乎在使用我的 TextEdit 时遇到了一些问题,所以我只是将其更改为 TextBlock,它现在是按钮的一部分且可单击。 WPF 鼠标事件是 RoutedEvent 并在可视化树中冒泡,但某些控件行为可能会停止事件冒泡。 在原生控件中,TextBox中的Button会拦截点击事件,点击TextBox不会触发Button点击事件。 请使用正确的控件来构建软件的 UI。

回答 2 投票 0

无法在自定义 Maui 控件中使用 GridLength 属性

我正在使用为我的 MAUI 项目制作的自定义控件。在此控件中,我有一个应该是 GridLength 类型的属性,但当我在项目中使用它时,它会引发错误:“值不能是

回答 2 投票 0

使用 ObservableGroupedCollection 时,Grouped CollectionView 标题文本不起作用

我需要一个分组集合视图,我想将其绑定到 ObservableGroupedCollection,因为我想更新数据并将我的更新反映在 UI 中。为了做到这一点...

回答 1 投票 0

AvaloniaUI 绑定到 ItemsSource 之外的命令

我有一个问题。我想向按钮提供来自 ViewModel 的命令,该命令位于 ItemsRepeater ItemsSource 之外。我需要有关如何进行此类绑定的帮助 我的 ItemsRepeater 中的按钮 我有一个问题。我想向按钮提供来自 ViewModel 的命令,该命令位于 ItemsRepeater ItemsSource 之外。我需要有关如何进行此类绑定的帮助 我的 ItemsRepeater 中的按钮 <Button Command={Binding TestClick} Grid.Column="0" HorizontalAlignment="Stretch" Foreground="#6264a7" HorizontalContentAlignment="Center" CornerRadius="0" Background="#2f2f2f" FontSize="20">Details</Button> 我的物品中继器 <ItemsRepeater.ItemTemplate> <DataTemplate> <DockPanel Margin="30,0,30,50"> <StackPanel> <TextBlock Background="#2f2f2f" FontSize="25" Foreground="AntiqueWhite" TextAlignment="Center" Padding="0,8,0,8" Text="{Binding Name}"></TextBlock> <TextBlock TextAlignment="Center" Background="#2f2f2f" Foreground="AntiqueWhite" Height="40" FontSize="20" Padding="0,8,0,0" Text="{Binding Date}"></TextBlock> <TextBlock TextAlignment="Center" Background="#2f2f2f" Foreground="AntiqueWhite" Height="40" FontSize="20" Padding="0,2,0,0" Text="{Binding EventType}"></TextBlock> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="50*" /> <ColumnDefinition Width="50*" /> </Grid.ColumnDefinitions> <Button Command={Binding TestClick} Grid.Column="0" HorizontalAlignment="Stretch" Foreground="#6264a7" HorizontalContentAlignment="Center" CornerRadius="0" Background="#2f2f2f" FontSize="20">Details</Button> <Button Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Foreground="#a4373a" CornerRadius="0" Background="#2f2f2f" FontSize="20">Delete</Button> </Grid> <ProgressBar Height="10" CornerRadius="0" Value="{Binding TimeLeft}" Minimum="0" Maximum="{Binding DifferenceBetweenDates}" Foreground="{Binding ProgressBarColour}" /> </StackPanel> </DockPanel> </DataTemplate> </ItemsRepeater.ItemTemplate> </ItemsRepeater> 我的视图模型: using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Anniverse.ViewModels { class MainPanelViewModel : ViewModelBase { public string CurrentDate => DateTime.Today.ToString("dd.MM.yyyy"); public ObservableCollection<Event> Events => new Connector().GetEvents(); public void TestClick() { Console.WriteLine("Hello test"); } } } 实现这一点的最简单方法是绑定到祖先 Command="{Binding $parent[ItemsRepeater].DataContext.YourCommand}" 请注意,您需要在视图模型中定义 ICommand 才能使命令绑定正常工作,您可以在 here 找到示例 编辑: 我没有意识到这一点,但直接绑定到方法应该也可以工作 我可能会迟到,但对于那些尝试@radoslawik 的答案但遇到此类错误的人 Unable to resolve property or method of name 'YourCommand' on type 'System.Object' 这是由于编译后的绑定无法知道数据上下文的真实类型造成的。 但是,有一个解决方法:通过强制转换: Command="{Binding $parent[ItemsControl].((vm:YouViewModel)DataContext).YourCommand}"

回答 2 投票 0

在 XAML 中设置和图像 CreateOptions 和 CacheOption

我想像这样设置我的 CreateOptions 和 CacheOption: img = 新的 BitmapImage(); img.BeginInit(); img.CacheOption = BitmapCacheOption.OnDemand; img.CreateOptions = BitmapCreateOptions.IgnoreImageC...

回答 2 投票 0

需要 SharpDXElement 替代品。 SharpDX WPF 闪烁的解决方法

我有一个 SharpDX 项目即将完成。它使用 Kinect 进行交互。因此,我的项目对 Kinect 区域和 KinectUserViewer 对象都使用 WPF。 SharpDX 有...

回答 5 投票 0

使用返回 StaticResource Primary 的转换器更改 xaml 中的颜色

如果我做这样的转换器 公共对象?转换(对象?值,类型目标类型,对象?参数,CultureInfo 文化) { 如果(值为空) 返回“粉红色”; 电子...

回答 1 投票 0


单击按钮 xaml 后如何在列表中生成一行

我想制作一个健身应用程序来记录你的锻炼情况。我试图列出一个列表,您可以在其中添加练习并设置次数和重量,但它不起作用。我尝试制作一个计数器(组),

回答 1 投票 0

XAML 设计器无法加载文件或程序集

我正在尝试提供设计时数据服务,以便我可以在 XAML 中使用一些数据。 我正在使用 OpenhardwareMonitor 库来获取 PC 的硬件信息。 我有代码

回答 2 投票 0

设置悬停在 GridView 列标题上的背景颜色

应该足够简单和常见:我想覆盖 GridView ColumnHeader 上的悬停颜色。 所以我使用 、a 和 a ,...

回答 1 投票 0

默认样式不适用于子类

我有一个奇怪的场景,涉及覆盖 WPF 中的默认样式并将它们应用于子类。这不可能吗?例如,我有以下代码: 我有一个奇怪的场景,涉及覆盖 WPF 中的默认样式并将它们应用于子类。这不可能吗?例如,我有以下代码: <Window x:Class="TestWPFStyling.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:testWpfStyling="clr-namespace:TestWPFStyling" Title="MainWindow" Height="350" Width="525" Background="White"> <Window.Resources> <Style TargetType="testWpfStyling:SomeLabel"> <Setter Property="Foreground" Value="Red" /> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <testWpfStyling:SomeLabel Grid.Row="0">This should be red.</testWpfStyling:SomeLabel> <testWpfStyling:YellowLabel Grid.Row="1">This should be red.</testWpfStyling:YellowLabel> <StackPanel Grid.Row="2"> <StackPanel.Resources> <Style TargetType="testWpfStyling:SomeLabel"> <Setter Property="Foreground" Value="Blue" /> </Style> </StackPanel.Resources> <testWpfStyling:SomeLabel>This should be blue.</testWpfStyling:SomeLabel> <testWpfStyling:YellowLabel>This should be blue.</testWpfStyling:YellowLabel> </StackPanel> </Grid> </Window> 使用此代码隐藏: namespace TestWPFStyling { public partial class MainWindow : Window { } public class SomeLabel : Label { } public class YellowLabel : SomeLabel { } } 我希望 StackPanel 中的控件 YellowLabel 的颜色为蓝色,外面的控件颜色为红色,但它们都是黑色的。有没有办法让子类采用其父类的默认样式? 实际上这就是 WPF 的工作原理。我有 一篇博客文章 讨论了 WPF 样式(主要是关于我们的 Theme 属性如何工作),但问题 1 的场景 1 的根本问题与您所描述的相同。也就是说,隐式本地样式是使用实际类类型定位的,与 DefaultStyleKey 无关。 DefaultStyleKey 仅在查找默认样式(即基于当前操作系统主题和控件的默认 generic.xaml 应用于控件的样式)时使用。解决这个问题的一种简单方法是将派生控件的样式(例如在其构造函数中)设置为对基类样式的 DynamicResource 引用。例如 this.SetResourceReference(StyleProperty, typeof(SomeLabel)); 您可以使用inherit样式BasedOn在YellowLabel上的样式,如下 <Window.Resources> <Style TargetType="local:SomeLabel"> <Setter Property="Foreground" Value="Red" /> </Style> <!-- Inherit the style for Label --> <Style TargetType="local:YellowLabel" BasedOn="{StaticResource {x:Type local:SomeLabel}}"></Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <local:SomeLabel Grid.Row="0">This should be red.</local:SomeLabel> <local:YellowLabel Grid.Row="1">This should be red.</local:YellowLabel> <StackPanel Grid.Row="2"> <StackPanel.Resources> <Style TargetType="local:SomeLabel"> <Setter Property="Foreground" Value="Blue" /> </Style> <!-- Inherit the style for Label --> <Style TargetType="local:YellowLabel" BasedOn="{StaticResource {x:Type local:SomeLabel}}"></Style> </StackPanel.Resources> <local:SomeLabel>This should be blue.</local:SomeLabel> <local:YellowLabel>This should be blue.</local:YellowLabel> </StackPanel> </Grid> 您得到所需的输出:

回答 2 投票 0

.NET MAUI Xaml:Flex 布局与图像和文本不一致

最近我对 xaml 感到非常沮丧,因为在 Flex 布局如何包装方面我无法获得形式上的一致性。我还没有找到包含内容的其他选项,因为我...

回答 1 投票 0

如何检测 FlowDocumentScrollViewer 视图中可见的部分?

我在 FlowDocument 中有多个部分,如下所示。由于 FlowDocument 内部有多个块,并且 FlowDocument 位于 FlowDocumentScrollViewer 内部,因此可以滚动内容。在这里,我...

回答 1 投票 0

动态更改 WPF 应用程序的布局以在网格上显示一两个组件

我有一个带有树列表主视图的 WPF 应用程序,我还希望能够在按下按钮时打开第二个组件(并在再次按下时隐藏它)。 但我无法制作第二个作品...

回答 1 投票 0

如何在 XAML 中将 FontAttributes 设置为粗体和斜体?

在 Xamarin.Forms 中,如何将 XAML 中的 FontAttributes 设置为粗体和斜体? 例子: <Setter Property="FontAttributes" Value="Bold" /> <...</desc> <question vote="16"> <p>在 Xamarin.Forms 中,如何将 <pre><code>FontAttributes</code></pre> 内的 <pre><code>XAML</code></pre> 设置为 <strong>粗体</strong> 和 <strong>斜体</strong>?</p> <p><strong>示例:</strong></p> <pre><code> &lt;Style TargetType=&#34;Label&#34;&gt; &lt;Setter Property=&#34;FontAttributes&#34; Value=&#34;Bold&#34; /&gt; &lt;Setter Property=&#34;FontAttributes&#34; Value=&#34;Italic&#34; /&gt; &lt;/Style&gt; </code></pre> </question> <answer tick="true" vote="30"> <pre><code>&lt;Style TargetType=&#34;Label&#34;&gt; &lt;Setter Property=&#34;FontAttributes&#34; Value=&#34;Bold, Italic&#34; /&gt; &lt;/Style&gt; </code></pre> <p><pre><code>FontAttributes</code></pre>是一个Flag,因此您可以传递多个值。</p> </answer> <answer tick="false" vote="4"> <p>如果有人正在寻找代码隐藏解决方案:</p> <pre><code>element.FontAttributes = FontAttributes.Bold | FontAttributes.Italic; </code></pre> </answer> <answer tick="false" vote="3"> <p>在值字段中继续使用逗号分隔它们。 </p> <pre><code>&lt;Style TargetType=&#34;Label&#34;&gt; &lt;Setter Property=&#34;FontAttributes&#34; Value=&#34;Bold, Italic&#34;/&gt; &lt;/Style&gt; </code></pre> <p>请务必查看 <a href="http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/" rel="nofollow">http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/</a></p> </answer> <answer tick="false" vote="0"> <pre><code>FontAttributes=&#34;Bold, Italic&#34; </code></pre> <p><a href="https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts</a></p> </answer> </body></html>

回答 0 投票 0

WPF 通过代码绑定到附加属性的嵌套属性

我必须创建一个 Binding 来读取对象属性的值,该对象的属性值是附加属性的值。 在 XAML 中我可以这样: 我必须创建一个 Binding 来读取对象的属性值,该属性值是附加属性的值。 在 XAML 中我可以这样: <TextBlock Text="{Binding Path=(myns:MyDependencyObject.MyAttachedProperty).NestedProperty, Mode=OneWay}"/> 如何在 C# 代码中做到这一点? 您可以通过以下方法做到这一点: var path = new PropertyPath("(0).NestedProperty", MyDependencyObject.MyAttachedProperty); var binding = new Binding() { Path = path, Mode = BindingMode.OneWay }; myTextBlock.SetBinding(TextBlock.TextProperty, binding); PropertyPath 使用的构造函数如下: public PropertyPath(string path, params object[] pathParameters) 请注意 "(0)" 与 string.Format 语法类似,其中 (0) 对应于 pathParameters 参数列表中的第一个参数。 微软文档对此不太清楚。

回答 1 投票 0

启动新窗口时如何关闭当前窗口(在代码中)

SignInWindow 登录= new SignInWindow(); 登录.ShowDialog(); 上面的代码位于我的 MainWindow 类中。 当显示新窗口时,我希望关闭当前窗口。最好的方法是什么...

回答 5 投票 0

XAML 中的 WPF 合并上下文菜单

是否可以在XAML中合并两个ContextMenu? 我创建了两个 ContextMenues 作为资源。我在几个数据模板中使用它们并且工作正常。但是,对于某些数据模板,我希望...

回答 1 投票 0

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