我有一个用户控件,它将样式应用于按钮,样式包含ControlTemplate部分.在ControlTemplate中,有各种UI元素,如Ellipse和Path.
如果我给这些元素 - Ellipse和Path - 一个带x:Name的名称,我可以从后面的代码中访问它们吗?
看起来样式的Ellipse和Path是不可见的,因为我收到编译错误(C#).
我是以错误的方式来做这件事的吗?
我正在尝试自定义WPF 4.0 DataGrid并需要其控件模板的列表(也是嵌套DataGridColumnHeadersPresenter的控件模板 - PART_ColumnHeadersPresenter).我发现此链接提供了许多控件模板,但列表中缺少DataGrid!是否有发布最新WPF 4.0 DataGrid的控件模板的地方.我尝试使用以下代码获取它,但它没有给我嵌套模板:
// Create an XmlWriter
StringBuilder sb = new StringBuilder();
XmlWriterSettings xmlSettings = new XmlWriterSettings
{ Indent = true, IndentChars = " ", NewLineOnAttributes = true };
XmlWriter writer = XmlWriter.Create(sb, xmlSettings);
// Write the control template
ControlTemplate template = controlToExtract.Template;
XamlWriter.Save(template, writer);
// Write results to display
controlTemplate.AppendText(sb.ToString());
Run Code Online (Sandbox Code Playgroud) 我想在列标题上方的现有WPF DataGrid中添加一个"drop"区域.在该区域中,我希望允许用户删除表示要分组的列的列标题,并通过从这个区域中删除它们来取消组合这些列.Xceed,SyncFusion和其他商业网格提供此功能,但我需要自己完成.
我的方法包括提取DataGrid的ControlTemplate,根据我的需要修改它,然后创建一个从DataGrid派生的新控件,它在generic.xaml中定义了这个ControlTemplate.在大多数情况下这是有效的,但必须有更好的方法,是吗?
我正在寻找一种使用附加属性的方法,但无法弄清楚如何将新的"drop"区域插入到现有控件的Visual Tree中.
欢迎任何建议.谢谢!
.
大家好,
我们可以用这样的方式编写自定义控件,以后我们可以对其进行部分重新模板化吗?意味着,在不更换整个ControlTemplate的情况下,我们想要改变/覆盖它的一些外观和感觉?
当然,一种方法是:从generic.xaml复制ControlTemplate代码,部分修改它的外观,并在xaml中使用它,如下所示:
<MyCustomControl>
<MyCustomControl.Template>
<ControlTemplate>
<!-- paste the copied and modified code from generic.xaml here-->
</ControlTemplate>
</MyCustomControl.Template>
<!--other code-->
</MyCustomControl>
Run Code Online (Sandbox Code Playgroud)
但是这种方法的问题在于,我们使xaml 过于冗长.我真的不喜欢它.我正在寻找一些解决方案,可以帮助我免受污染我正在使用自定义控件的xaml.
在期待中感谢!
.
我想为WPF组合框创建一个平面样式模板,它看起来像Visual Studio 2010中的组合框.此外,我想使用Visual Studio画笔来完成此模板.

有人帮我找路吗?有没有完成的模板?也有人知道一个可以从其他应用程序获取控件模板的应用程序吗?
我有自己的UserControl,LabeledTextBox它是a Label和a..well 的组合,TextBox.此控制具有两个属性:Caption这将被绑定到的标题Label,和Value将被绑定到Text的TextBox.
码:
public class LabeledTextBox : Control
{
static LabeledTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledTextBox), new FrameworkPropertyMetadata(typeof(LabeledTextBox)));
}
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
// Using a DependencyProperty as the backing store for Caption. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata(""));
public string Value
{
get { …Run Code Online (Sandbox Code Playgroud) 我有以下用于WPF ListBox控件的xaml.我已经定制了它以满足我的UI要求.但是,滚动条滑块不会到达滚动条的顶部和底部.如果我点击垂直滚动条轨道上的其他位置,拇指也不会滚动.请帮我解决问题.任何帮助表示赞赏.
这是我定制的ListBox控件的快照:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="MovieMessageBox.ListBoxStyles2"
x:Name="Window"
Title="ListBoxStyles2"
Width="370" Height="200">
<Window.Resources>
<SolidColorBrush x:Key="ListBorder" Color="#828790"/>
<Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid>
<Rectangle Fill="Pink" RadiusX="10" RadiusY="10" Opacity="0.195" />
<Border CornerRadius="10" x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1" SnapsToDevicePixels="true">
<ScrollViewer Focusable="false" Padding="{TemplateBinding …Run Code Online (Sandbox Code Playgroud) 我覆盖了 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什么区别?
好吧,所以我有一个窗口,其中包含以下资源
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<TextBlock Text="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息,说"会员"内容"无法识别或无法访问." 我究竟做错了什么?
我在试图实现一件非常微不足道的事情时感到非常沮丧(或者至少,我期望的事情应该是微不足道的......)
我有一个需要自定义切换按钮的要求,为此我需要创建一个托管切换按钮的用户控件,并托管该用户控件中描述的内容。我制作了一个小型迷你应用程序来演示“要求”。
<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 ×10
wpf ×9
styles ×3
c# ×2
datagrid ×2
combobox ×1
data-binding ×1
grouping ×1
listbox ×1
scrollbar ×1
silverlight ×1
uwp ×1
xaml ×1