wpf 相关问题

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

Mahapps Metro - Expander:将 LayoutTrasform 应用到标题更改样式

在使用 Mahapps Metro 的 WPF 应用程序中,我想在右侧使用 Expander。 为了防止 Expander 在折叠时占用太多空间,我想将 layoutTrasform 应用于其标题。

回答 1 投票 0

WPF 应用程序退出而不保存数据,尽管没有抛出异常

我有一个使用 MahApps.Metro 的 WPF 应用程序,并带有一个标题栏关闭按钮来关闭该应用程序。我的目标是在应用程序关闭时将记录存储在 Azure 存储帐户中。我已经

回答 1 投票 0

WPF绑定DataGrid模板列组合框CellEditingTemplate和文本CellTemplate

我有一个产品集合 { SuppliesId, SupplyProductName } 我还有一张连接到此产品列表的发票 ProductInvoice {InvoiceId, SupplyProductId } 我创建了一个 WPF Da...

回答 1 投票 0

为组合框创建用户定义的列表(EF、WPF、MVVM、C#)?

我有一个名为 Project 的对象。该项目有一个名为 ProjectState 的字段。过去这个字段是一个枚举,所以很容易处理。它作为 ItemSource 绑定到 ComboBox,什么都没有

回答 1 投票 0

为组合框(EF、WPF、MVVM、C#)创建用户定义列表的最佳方法?

我有一个名为 Project 的对象。该项目有一个名为 ProjectState 的字段。过去这个字段是一个枚举,所以很容易处理。它作为 ItemSource 绑定到 ComboBox,什么都没有

回答 1 投票 0

从 UserControl 公开 ComboBox.ItemsSource

我创建了一个简单的 WPF UserControl,其中包含一个 ComboBox,我想公开后者的 ItemsSource 属性以从父视图绑定集合。 这是 xaml ...

回答 1 投票 0

WPF 绑定在 MahApps.Metro 中出现一些错误,但它可以运行

**在我的xaml中:** **在我的xaml中:* * <! -- Items --> <Controls: HamburgerMenu. ItemsSource> <Controls: HamburgerMenuItemCollection> <Controls: HamburgerMenuItem Command="{Binding LinkViewModel. isMVIRCommand}" Label="WVIR"></Controls: HamburgerMenuItem> <Controls: HamburgerMenuItem Command="{Binding LinkViewModel. isTemperatureCommand}" Label="temperature"></Controls: HamburgerMenuItem> </Controls: HamburgerMenuItemCollection> </Controls: HamburgerMenu. ItemsSource> **In my LinkViewModel: ** public DelegateCommand isMVIRCommand { get; set; } public DelegateCommand isTemperatureCommand { get; set; } isMVIRCommand = new DelegateCommand(isMVIR); isTemperatureCommand = new DelegateCommand(ChangeToTemperature); private void ChangeToTemperature() { isMV = "Collapsed"; isIR = "Collapsed"; isTemperature = "Visible"; } private void isMVIR() { isMV = "Visible"; isIR = "Collapsed"; isTemperature = "Collapsed"; } **follow it's wrong: ** LinkViewModel. isMVIRCommand HamburgerMenuItem. Command ICommand FrameworkElement or FrameworkContentElement can't find the target element management. LinkViewModel. IsTemperatureCommand HamburgerMenuItem.Com and ICommand FrameworkElement or can't find the target element management FrameworkContentElement. 我确信它已经在 LinkViewModel 中 Binding 了,但是当我运行我的项目时,它可以按照我的预期运行,但是我的 wpf 设计器搞错了我。希望有人能帮我找出问题所在 我知道我可以运行该项目的原因,但我的wpf设计师对我有误解。请阅读我的代码。 <Controls:HamburgerMenuItem Command="{Binding DataContext.LinkViewModel.isMVIRCommand, RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}}" Label="WVIR"/> <Controls:HamburgerMenuItem Command="{Binding DataContext.LinkViewModel.isTemperatureCommand, RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}}" Label="temperature"/> 我添加了RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}。他们告诉wpf设计器在可以找到MetroWindow时搜索设计器树。在我的情况下,我的MetroWindow DataContext已设置为MainWindowViewModel ,而我的LinkViewModel是MainWindowModel中的一个元素,所以可以在WPF设计器中找到它

回答 1 投票 0

按钮奇怪的背景结束了?

我正在使用这个按钮: ... 我正在用这个Button: <Button Name="buttonAttiva" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,560,0,0"> <TextBlock Text="Attiva" /> 就是这样的风格: <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MahApps.Styles.Button}"> <Setter Property="Width" Value="120" /> <Setter Property="Height" Value="46" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="Background" Value="#81b3d4" /> <Setter Property="Foreground" Value="#ffffff" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontSize" Value="18" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="controls:ControlsHelper.MouseOverBorderBrush" Value="Black" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Orange" /> <Setter Property="BorderThickness" Value="2" /> <Setter Property="BorderBrush" Value="Black" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="Orange" /> <Setter Property="BorderThickness" Value="2" /> <Setter Property="BorderBrush" Value="Black" /> </Trigger> </Style.Triggers> </Style> 正确显示为: 但是,当我用鼠标悬停时,会出现奇怪的“灰色”背景: 如何设置正确的橙色背景? 注意,我正在使用 MahApps.Metro,这可能会覆盖我猜的一些属性? 覆盖模板中使用的MahApps.Brushes.Gray8资源: <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MahApps.Styles.Button}"> <Style.Resources> <SolidColorBrush x:Key="MahApps.Brushes.Gray8" Color="Orange" /> </Style.Resources> ...

回答 1 投票 0

如何为 2 个嵌套列表框 WPF C# 添加双击事件

<Window x:Class="SecutiryQuizApp.Views.Windows.CreateOrEditInspection" 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:local="clr-namespace:SecutiryQuizApp.Views.Windows" xmlns:contols="clr-namespace:SecutiryQuizApp.UserControls" xmlns:converter="clr-namespace:SecutiryQuizApp.Converters" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" mc:Ignorable="d" Title="Обход" Height="600" Width="600" Icon="/Resources/inspections.ico"> <Window.Resources> <converter:TypeAnswerConverter x:Key="TypeAnswerConverter"/> <converter:BooleanInverterConverter x:Key="BooleanInverterConverter"/> <converter:Base64ToBitmapImageConverter x:Key="Base64ToBitmapImageConverter"/> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="50*"/> <RowDefinition Height="519*"/> <RowDefinition Height="15*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="20*"/> <ColumnDefinition Width="564*"/> <ColumnDefinition Width="20*"/> </Grid.ColumnDefinitions> <ScrollViewer Grid.Column="1" Grid.Row="1"> <Grid Margin="5,5,5,5"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ComboBox Grid.Row="1" ItemsSource="{Binding QuizCollection}" SelectedItem="{Binding SelectedQuiz, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,10,0,0" materialDesign:HintAssist.Hint="Выберите опрос..." FontFamily="Bahnschrift" FontSize="16" Background="White" IsEnabled="{Binding VisibilityDonut, Converter={StaticResource BooleanInverterConverter}}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <ListBox x:Name="QuestionsList" Grid.Row="3" ItemsSource="{Binding QuestionsCollection}" Margin="0,10,0,10" FontSize="16" HorizontalAlignment="Stretch" IsEnabled="{Binding VisibilityDonut, Converter={StaticResource BooleanInverterConverter}}" > <ListBox.Template> <ControlTemplate TargetType="{x:Type ListBox}"> <ScrollViewer CanContentScroll="False"> <ItemsPresenter /> </ScrollViewer> </ControlTemplate> </ListBox.Template> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Height" Value="auto"/> <Setter Property="Background" Value="White"/> <Setter Property="Margin" Value="0 -2 0 0"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/> <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="3" CornerRadius="7"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="10"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="10"/> </Grid.ColumnDefinitions> <Grid Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Text="{Binding Description}" Grid.Row="0" Grid.Column="0" TextTrimming="CharacterEllipsis" Name="name" FontWeight="Medium" Foreground="Black" VerticalAlignment="Top" Margin="10 5 0 0" FontSize="15" FontFamily="Bahnschrift"/> <TextBlock Opacity="0" Grid.Column="1" Grid.Row="0" Width="Auto" Text="{Binding idTypeAnswer, Converter={StaticResource TypeAnswerConverter}}" MaxHeight="30" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Name="description" FontWeight="SemiBold" Foreground="Black" VerticalAlignment="Top" Margin="10 7 0 0" FontSize="12" FontFamily="Bahnschrift"/> <ListBox Grid.Row="1" x:Name="ImagesList" ItemsSource="{Binding ImagePathsCollection}" MaxHeight="120" FontSize="16" Grid.ColumnSpan="2"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Border BorderBrush="Transparent" CornerRadius="15"> <Grid> <Image Source="{Binding Converter={StaticResource Base64ToBitmapImageConverter}}" Width="35" Height="35"/> </Grid> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <ListBox Grid.Row="2" Margin="5,5,5,5" ItemsSource="{Binding Answers}" x:Name="ListAnswers" Grid.ColumnSpan="2" Background="LightYellow"> <ListBox.Template> <ControlTemplate TargetType="{x:Type ListBox}"> <ScrollViewer CanContentScroll="False"> <ItemsPresenter /> </ScrollViewer> </ControlTemplate> </ListBox.Template> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Height" Value="40"/> <Setter Property="Background" Value="LightYellow"/> <Setter Property="Margin" Value="0 -2 0 0"/> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/> <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="3" CornerRadius="7"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="10"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="40"/> </Grid.ColumnDefinitions> <Grid Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <TextBlock Text="Ответ: " FontSize="20" FontFamily="Bahnschrift" Grid.Column="0" VerticalAlignment="Center"/> <TextBlock Text="{Binding Answer}" FontSize="20" FontFamily="Bahnschrift" Grid.Column="1" VerticalAlignment="Center"/> </Grid> <Button Grid.Column="2" Background="Red" Width="20" Height="20" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,5,5,0" Command="{Binding DataContext.RemoveMarkCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" CommandParameter="{Binding}"> <Button.Template> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20"> <iconPacks:PackIconMaterial Width="10" Height="10" VerticalAlignment="Center" HorizontalAlignment="Center"> <iconPacks:PackIconMaterial.Style> <Style TargetType="iconPacks:PackIconMaterial"> <Setter Property="Kind" Value="close"/> </Style> </iconPacks:PackIconMaterial.Style> </iconPacks:PackIconMaterial> </Border> </ControlTemplate> </Button.Template> </Button> </Grid> </Border> <ControlTemplate.Triggers> <MultiTrigger> <Setter Property="Background" Value="LightYellow"/> </MultiTrigger> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="LightYellow"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="LightGray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewMouseDoubleClick"> <i:InvokeCommandAction Command="{Binding DataContext.OpenAnswerEditCommand, ElementName=ListAnswers}" CommandParameter="{Binding}"/> </i:EventTrigger> </i:Interaction.Triggers> </ListBox> </Grid> </Grid> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True"/> </MultiTrigger.Conditions> <MultiTrigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="description" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" From="0" To="1" Duration="0:0:0.2" /> </Storyboard> </BeginStoryboard> </MultiTrigger.EnterActions> <MultiTrigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="description" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" From="1" To="0" Duration="0:0:0.2" /> </Storyboard> </BeginStoryboard> </MultiTrigger.ExitActions> <Setter Property="Background" Value="#edeaf4"/> </MultiTrigger> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="#eff3fb"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#FFECECEC"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDoubleClick"> <i:InvokeCommandAction Command="{Binding EditOrAddQuestionCommand}" CommandParameter="{Binding SelectedItem, ElementName=QuestionsList}"/> </i:EventTrigger> </i:Interaction.Triggers> </ListBox> <Button Grid.Row="4" Content="Сохранить изменения" HorizontalAlignment="Stretch" VerticalAlignment="Top" Command="{Binding SaveQuizCommand}" Margin="0,10,0,10" Background="#FF2196F3" BorderBrush="#FF2196F3" Foreground="White" FontFamily="Bahnschrift" FontSize="16" IsEnabled="{Binding VisibilityDonut, Converter={StaticResource BooleanInverterConverter}}"/> </Grid> </ScrollViewer> <Grid Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="119*"/> <ColumnDefinition Width="37*"/> <ColumnDefinition Width="53*"/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" FontFamily="Bahnschrift" FontSize="22" TextAlignment="Center" Margin="0,0,0,5"><Run Language="ru-ru" Text="Обход"/></TextBlock> <Button Grid.Column="3" Content="Удалить обход" VerticalAlignment="Bottom" Command="{Binding RemoveQuizCommand}" Background="#FFFF4C30" BorderBrush="#FF2196F3" Foreground="White" FontFamily="Bahnschrift" FontSize="16" IsEnabled="{Binding VisibilityDonut, Converter={StaticResource BooleanInverterConverter}}" Grid.ColumnSpan="2" Margin="0,0,0,5"/> </Grid> <contols:DonutSpinner Grid.Row="1" Visibility="{Binding VisibilityDonut, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}" Grid.Column="1" VerticalAlignment="Stretch" MaxWidth="80"/> </Grid> </Window> using SecutiryQuizApp.Models; using SecutiryQuizApp.ViewModels; using System.Windows; namespace SecutiryQuizApp.Views.Windows { public partial class CreateOrEditInspection : Window { public CreateOrEditInspection(InspectionAnswer inspection) { InitializeComponent(); this.DataContext = new OpenOrEditInspectionVM(inspection); } } } public RelayCommand OpenAnswerEditCommand { get { return new RelayCommand(parameter => { if (parameter is InspectionAnswer question) { OpenEditAnswer(question); } }); } } private InspectionAnswer inspection { get; set; } public OpenOrEditInspectionVM(InspectionAnswer _inspection) { inspection = _inspection; Runner(); } 我遇到了 PreviewMouseDoubleClick OpenAnswerEditCommand 事件在我的 WPF 应用程序中无法按预期工作的问题。我有一个名为 ListAnswers 的列表框,我想在双击 ListAnswers 中的 ListBoxItem 时触发视图模型中的 OpenAnswerEditCommand 命令。然而,尽管在XAML中设置了事件触发器,但该命令似乎没有被执行。 绑定到第二个 DataContext 父级的 ListBox: <i:InvokeCommandAction Command="{Binding DataContext.OpenAnswerEditCommand, RelativeSource={RelativeSource AncestorType=ListBox, AncestorLevel=2}}" CommandParameter="{Binding}"/> 或者父窗口: <i:InvokeCommandAction Command="{Binding DataContext.OpenAnswerEditCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding}"/> 此处不能使用 ElementName,因为嵌套的 ListBox 与外部的名称范围不同。

回答 1 投票 0

MediaPlayer.Open 播放打开时的声音

我有这个: MediaPlayer[] 声音 = new MediaPlayer[40]; 私有无效 LoadSamples() { 对于 (int i = 0; i < sounds.Length; i++) { try { sounds[i].Open(new Uri([my path here])); } ...

回答 1 投票 0

设置背景禁用MouseOver效果

这是我的风格: <Style.Triggers> ...</desc> <question vote="1"> <p>这是我的风格:</p> <pre><code> &lt;UserControl.Resources&gt; &lt;Style TargetType=&#34;Button&#34; x:Key=&#34;Style2&#34; BasedOn=&#34;{StaticResource MetroCircleButtonStyle}&#34;&gt; &lt;Style.Triggers&gt; &lt;Trigger Property=&#34;IsMouseOver&#34; Value=&#34;True&#34;&gt; &lt;Setter Property=&#34;Foreground&#34; Value=&#34;#000000&#34;/&gt; &lt;Setter Property=&#34;Background&#34; Value=&#34;#ededed&#34;/&gt; &lt;/Trigger&gt; &lt;Trigger Property=&#34;IsPressed&#34; Value=&#34;True&#34;&gt; &lt;Setter Property=&#34;Foreground&#34; Value=&#34;#ffffff&#34;/&gt; &lt;Setter Property=&#34;Background&#34; Value=&#34;#009688&#34;/&gt; &lt;/Trigger&gt; &lt;Trigger Property=&#34;IsEnabled&#34; Value=&#34;false&#34;&gt; &lt;Setter Property=&#34;Foreground&#34; Value=&#34;#FF808080&#34;/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>我这样使用它:</p> <pre><code>&lt;Button x:Name=&#34;button&#34; Content=&#34;abcdefg&#34; Style=&#34;{DynamicResource Style2}&#34; Width=&#34;45&#34; Height=&#34;45&#34; VerticalAlignment=&#34;Top&#34; BorderThickness=&#34;0&#34; FontSize=&#34;18&#34; FontFamily=&#34;/WpfApplication2;component/Resources/#Vazir&#34;/&gt; </code></pre> <p>这个效果非常好。问题是当我尝试通过更改 XAML 或动态更改背景来更改按钮的背景时。当我这样做时,鼠标悬停效果不再起作用。</p> <pre><code>&lt;Button x:Name=&#34;button&#34; Content=&#34;abcdefg&#34; Background=&#34;White&#34; Style=&#34;{DynamicResource Style2}&#34; Width=&#34;45&#34; Height=&#34;45&#34; VerticalAlignment=&#34;Top&#34; BorderThickness=&#34;0&#34; FontSize=&#34;18&#34; FontFamily=&#34;/WpfApplication2;component/Resources/#Vazir&#34;/&gt; </code></pre> <p>或者</p> <pre><code>button.Background = Brushes.Transparent; </code></pre> <p>设置背景时MouseOver效果不起作用,但我需要设置背景。怎么了?</p> </question> <answer tick="true" vote="2"> <p>直接设置的依赖属性值(“本地值”)比样式设置器设置的值具有更高的优先级。有关详细信息,请参阅<a href="https://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx" rel="nofollow noreferrer">依赖属性值优先级</a>。</p> <p>所以,设置</p> <pre><code>&lt;Button Background=&#34;White&#34; ... /&gt; </code></pre> <p>有效禁用您的触发器。</p> <p>您应该通过另一个样式设置器来设置 <pre><code>Background</code></pre> 属性,例如 </p> <pre><code>&lt;Button ...&gt; &lt;Button.Style&gt; &lt;Style TargetType=&#34;Button&#34; BasedOn=&#34;{StaticResource Style2}&#34;&gt; &lt;Setter Property=&#34;Background&#34; Value=&#34;White&#34;/&gt; &lt;/Style&gt; &lt;/Button.Style&gt; &lt;/Button </code></pre> </answer> <answer tick="false" vote="1"> <p>如果您希望能够将背景属性设置为本地值,并且当鼠标移动到按钮上时仍然可以更改它,您可以在按钮的 ControlTemplate 中定义触发器。这实际上意味着您必须复制 MetroCircleButtonStyle 基本样式或将数据触发器直接添加到基本样式。</p> <p>或者,您可以按照 Clemens 的建议在 Style 中设置 Foreground 属性。您可以像这样以编程方式创建样式:</p> <pre><code>Style style = new Style { TargetType = typeof(Button) }; style.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Green)); style.BasedOn = FindResource(&#34;MetroCircleButtonStyle&#34;) as Style; button1.Style = style; </code></pre> </answer> <answer tick="false" vote="1"> <p>实际上,您可以使用优先级高于“本地值”的动画来做到这一点,具体方法如下:</p> <p>像这样定义你的风格:</p> <pre><code> &lt;Style TargetType=&#34;Button&#34; x:Key=&#34;ButtonStyle&#34;&gt; &lt;Style.Triggers&gt; &lt;EventTrigger RoutedEvent=&#34;Button.MouseEnter&#34;&gt; &lt;BeginStoryboard&gt; &lt;Storyboard TargetProperty=&#34;(Foreground).(SolidColorBrush.Color)&#34;&gt; &lt;ColorAnimation To=&#34;Red&#34; Duration=&#34;0:0:0&#34;/&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/EventTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>然后你就可以使用你的按钮了</p> <pre><code> &lt;Button Content=&#34;Button Title 1&#34; Foreground=&#34;Green&#34; Style=&#34;{StaticResource ButtonStyle}&#34;/&gt; &lt;Button Content=&#34;Button Title 2&#34; Foreground=&#34;Blue&#34; Style=&#34;{StaticResource ButtonStyle}&#34;/&gt; </code></pre> <p>此“技巧”用于标准 WPF 控件样式和模板。</p> </answer> </body></html>

回答 0 投票 0

WPF鼠标悬停触发器不改变按钮的背景颜色

我知道这是重复的,但即使在尝试复制我在类似帖子中看到的代码后,我似乎仍然无法让按钮在悬停时更改其背景颜色。 这是 XAML。 <

回答 1 投票 0

如何控制工具窗口的搜索控件的位置?

我正在开发 WPF Visual Studio 2022 扩展。它包含一个 ToolWindowPane 和一个我正在开发的自定义 UserControl。我想将“标准”VS 搜索控件添加到我的控件中...

回答 1 投票 0

WPF - 数据绑定 - 数据未显示在自定义用户控件中

我有一个用户控件,我尝试进行简单的绑定工作 无论我做什么,当我运行应用程序时,数据都不会显示在控件中 公共部分类 UserControlA: UserControl { ...

回答 1 投票 0

可能会出现什么问题?我从 ObservableCollection<T> 的 OnCollectionChanged()

我当前的项目围绕读取、修改和写入字节集合。我正在通过使用 ObservableCollection 并订阅其

回答 1 投票 0

WPF/C# - dropbox 样式 - 无法单击 ToggleButton 背景

我到处收集代码来制作自定义组合框 但我只能点击箭头来选择一个项目 我希望能够单击整个 ToggleButton,而不仅仅是箭头 由于某种原因

回答 1 投票 0

同一行中的元素左右对齐

在我使用代码隐藏的 WPF 应用程序中,我正在使用边框创建一些卡片,在其中添加几个元素,并且我试图使第一行的文本向左对齐,并有一个按钮 ali...

回答 1 投票 0

在 WPF 应用程序中使用 FLAUI Inspect 工具检查时,如果数据不可见,如何从列表元素中检索数据?

这个问题已经解决了。滚动下方寻找答案 我正在研究 UI 自动化,并且是 FLAUI 的新手。在这里,我希望提取用橙色笔注释的数据。该框突出显示...

回答 1 投票 0

在 WPF .NET 6 中使用 C++/WinRT 组件(但实际上是任何 WinRT 组件)

我想编写一个 WPF 应用程序,但尽可能在现代框架上构建它,包括 .NET 6 甚至更高版本。 我有一些我想调用的 C++ 代码,我喜欢

回答 1 投票 0

保存/加载数据

你好,我正在尝试为学校项目制作一个 cookie 点击器,但我不知道如何将多个数据保存到你的程序中,你可以在此处的 c# WPF 下找到保存/加载数据 私人空间

回答 1 投票 0

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