我覆盖了 ResourceDictionary 中控件的模板Generic.xaml。我添加了一个按钮,我想在上面添加一些事件。
<Setter Property="Template">
<Setter.Value>
--Added my button here.
</Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)
所以在加载控制事件中我做了
Button b = (Button)mycontrol.Template.FindName("PARTName", mycontrol)
//Add Events on my button
Run Code Online (Sandbox Code Playgroud)
我在互联网上读到的一些我可以做的地方
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
UIElement editingelement = GetTemplateChild("PART_EditingElement");
if (editingelement != null)
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试做这个建议时GetTemplateChild说不要使用。

所以我的问题是
为什么不使用GetTemplateChild. 它过时了吗?和
FrameWorkTemplate.FindName和 和有ControlTemplate.FindName什么区别?
我在试图实现一件非常微不足道的事情时感到非常沮丧(或者至少,我期望的事情应该是微不足道的......)
我有一个需要自定义切换按钮的要求,为此我需要创建一个托管切换按钮的用户控件,并托管该用户控件中描述的内容。我制作了一个小型迷你应用程序来演示“要求”。
<local:MyUserControl1>
<TextBlock>Just an example</TextBlock>
</local:MyUserControl1>
Run Code Online (Sandbox Code Playgroud)
外观MyUserControl1如下:
<UserControl
x:Class="App2.MyUserControl1"
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"
mc:Ignorable="d" Name="Bla" d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<Style TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Ellipse Width="300" Height="300" Fill="Blue"/>
<ContentPresenter Content="{Binding ElementName=Bla, Path=MainContent}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<ToggleButton/>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
背后代码:
public static DependencyProperty MainContentProperty = DependencyProperty.Register(
"MainContent",
typeof(object),
typeof(MyUserControl1),
null);
public object MainContent
{
get => GetValue(MainContentProperty);
set => SetValue(MainContentProperty, value);
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,会显示文本,但样式/切换按钮被忽略/未应用/无论如何。
视觉树确认我做错了什么:
我已经查看了许多其他相关的 SO 问答,但我仍然不知道如何让它按照我想要的方式工作。
我想在运行时定义一个ControlTemplate.这可能吗?我注意到ControlTemplate类上的VisualTree属性.我还注意到它使用了FrameworkElementFactory类.但是,我似乎无法让它发挥作用.
是否可以在运行时创建ControlTemplate?
我试图在App.xaml中定义一个全局按钮样式,它主要按照我的预期工作.但是,我只是想不通如何让Foreground正常工作.无论我做什么,我都会得到默认TextBlock的样式(将颜色设置为白色).
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="3, 5" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="FocusVisualStyle"
Value="{StaticResource ButtonFocusVisual}" />
<Setter Property="Foreground" Value="Red" />
<Setter Property="Padding" Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="gridMainButton"
RenderTransformOrigin="0.5, 0.5">
<Grid.RenderTransform>
<ScaleTransform x:Name="scaleTransform"
CenterX="0.5"
CenterY="0.5" />
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates" >
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="scaleTransform"
Storyboard.TargetProperty="ScaleX"
Duration="0"
To="0.85" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="ellipse"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
StrokeThickness="2"
Stroke="{StaticResource standardBackground}"
Fill="{StaticResource standardBackground}" />
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="4, 8"/> …Run Code Online (Sandbox Code Playgroud) 我想添加一个小按钮,它删除TextBox中的所有文本.是否可以将"删除"-Button放入TextBox(就像在iPhone文本框中一样)?
我希望在你的帮助之后看起来像这样:

我用controltemplate玩了一些东西,但没有得到希望的结果.
解决这个问题的一种方法可能是使用按钮的负边距,但我认为这不是一个干净的解决方案.
谢谢!
我很难让RowStyleSelector与WPF DataGrid一起使用.
在我的资源中,我有
<loc:DetailsRowStyleSelector x:Key="detailsRowStyleSelector" AddRowStyle="{StaticResource newItemRowStyle}" StandardRowStyle="{StaticResource RowStyle}"/>
Run Code Online (Sandbox Code Playgroud)
然后我的datagrid像这样使用它:
<DataGrid ...
EnableRowVirtualization="false"
VirtualizingStackPanel.VirtualizationMode="Standard"
RowStyleSelector="{StaticResource detailsRowStyleSelector}"
Run Code Online (Sandbox Code Playgroud)
调用选择器的构造函数,但SelectStyle方法不是,我的行看起来都一样.关于这一点的文档似乎很少,但这就是我的选择器的样子:
public class DetailsRowStyleSelector : StyleSelector {
public Style AddRowStyle { get; set; }
public Style StandardRowStyle { get; set; }
public DetailsRowStyleSelector() {
Console.WriteLine(""); // this is called
}
public override Style SelectStyle(object item, DependencyObject container) {
// this is not called
Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义的上下文菜单,我在其中更改了所有项目的外观.这些项目包含不同的控件,如组合框和按钮.现在我希望在按下按钮或选择组合框项目时关闭菜单.目前菜单仍然保持打开状态.你能给我一个提示吗?
这是一个简化的代码来显示我做了什么:
<ContextMenu StaysOpen="False">
<MenuItem>
<MenuItem.Template>
<ControlTemplate>
<Grid MinWidth="200">
<Button Command="{Binding SomeWorkingCommandBinding}">OK</Button>
</Grid>
</ControlTemplate>
</MenuItem.Template>
</MenuItem>
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
如上所述,当我点击OK按钮时,我想关闭菜单.
UPDATE
以下按钮(或任何其他控件)无需使用Blend SDK即可完成此操作:
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(ContextMenu.IsOpen)" Storyboard.Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}}">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>False</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
Run Code Online (Sandbox Code Playgroud) 我想访问嵌套在样式的控件模板内的属性。我知道您可以在后面的代码中执行此操作:
GradientStop stop = (GradientStop)progressBar1.Template.FindName("gradStop", progressBar1);
stop.Color = Colors.Black;
Run Code Online (Sandbox Code Playgroud)
是否可以这样做,但是在XAML中呢?例如:
<ProgressBar Style="{StaticResource CustomProgressBar}" [???].Color="FF000000"/>
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种定义WPF资源(目前用作静态资源)的方法,该资源可从我的应用程序中的任何位置访问。
我的资源在此资源字典中定义:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="flatButtonStyle" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="4"/>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
选择此问题的答案表明,我必须将该资源字典合并到我的App.xaml文件中(名为的项目AppWideResources):
<Application x:Class="AppWideResources.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AppWideResources;component/CommonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
起初,这似乎有效;该窗口中的按钮以适当的扁平样式设计,带有超厚边框:
<Window x:Class="AppWideResources.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AppWideResources" Height="300" Width="300"
>
<StackPanel>
<Button Style="{StaticResource flatButtonStyle}" Content="Test" HorizontalAlignment="Stretch"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
但是,一旦在控件模板中使用共享资源,此操作就会停止:
我(出于此问题的目的,对此进行了极其简化)控制:
using System;
using System.Windows;
using System.Windows.Controls;
namespace AppWideResources
{
public class MyControl : Control
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
} …Run Code Online (Sandbox Code Playgroud) 考虑这个(编辑降)Style,设计用于Button其Content是String:
<Style x:Key="Test" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<StackPanel>
<TextBlock x:Name="text" Text="{TemplateBinding Content}" />
<TextBlock x:Name="demo" Text="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}">
<DataTrigger.Value>
<system:String>Test</system:String>
</DataTrigger.Value>
<Setter TargetName="test" Property="Foreground" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
此示例中的意图是,如果按钮文本等于单词"Test" 1,则将其变为红色.但是,这是行不通的,因为触发的TemplatedParent绑定解析为空,而不是到Button了Style被应用到.但是,TextBlock命名的"demo"将按Text预期设置为"System.Windows.Controls.Button:[ButtonText]",这意味着TemplatedParent在该级别上正常工作.为什么它不在里面工作DataTrigger?
controltemplate ×10
wpf ×8
xaml ×4
c# ×3
styles ×2
.net ×1
app.xaml ×1
contextmenu ×1
controls ×1
datagrid ×1
resources ×1
uwp ×1
wpf-controls ×1
wpf-style ×1