我有ControlTemplate一些Paths.我希望Paths伸展并填充它们所处的控制,例如a Button.我怎样才能做到这一点?
我目前看起来像这样:
<ControlTemplate x:Key="SomeTemplate" TargetType="Button">
<Canvas Background="AliceBlue">
<Path Data="M 99.5,50 A 49.5,49.5 0 1 1 0.5,50 A 49.5,49.5 0 1 1 99.5,50 z"
Fill="White" Stroke="Black" StrokeThickness="1" />
<Path Data="M 15,50 C 17.5,22.5 47.5,22.5 50,50 C 52.5,77.5 82.5,77.5 85,50"
Stroke="Black" StrokeThickness="1" />
</Canvas>
</ControlTemplate>
...
<Button Template="{StaticResource SomeTemplate}" Height="120" Width="120" />
Run Code Online (Sandbox Code Playgroud)
我知道ScaleTransform的StrechX和StretchY属性,但它们只有原来的比例缩放Path的尺寸.
我会使用值转换器吗?或者也许某种形式的相对约束父母的大小?
为什么以下简化代码没有将TextBlock的字体大小设置为50?
<Window.Resources>
<ControlTemplate TargetType="ContentControl" x:Key="Test">
<ContentPresenter TextBlock.FontSize="50" />
</ControlTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource Test}">
<TextBlock>Test should be rendered big</TextBlock>
</ContentControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
如果我更改FontSize属性的值,visual studio会向我显示我想要的大小的文本.编译或执行应用程序后,文本块的大小始终重置为其默认大小.
我还测试了各种版本的样式和嵌入式资源,但我总是处于这样的情况:我无法在包含ContentPresenter的ControlTemplate中继承附加的dp.这是设计的吗?
我想创建一个像一个控件Panel.
我希望我的控件接受一些控件作为孩子而不键入模板名称,就像Panel,如下所示:
<asp:Panel runat="server">
My content
<div>Content</div>
</asp:Panel>
Run Code Online (Sandbox Code Playgroud)
我有内容控件,而不知道是什么ITemplate.
我基本上想转换它
<my:MyControl runat="server">
<ContentTemplate>
My content
<div>Content</div>
</ContentTemplate>
</my:MyControl>
Run Code Online (Sandbox Code Playgroud)
进入这个
<my:MyControl runat="server">
My content
<div>Content</div>
</my:MyControl>
Run Code Online (Sandbox Code Playgroud)
这是我得到的:
public class MyControl : CompositeControl
{
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content { get; set; }
protected override void CreateChildControls()
{
base.CreateChildControls();
Content.InstantiateIn(this);
}
}
Run Code Online (Sandbox Code Playgroud)
以上工作与<Content></Content>控件内的标签,但没有它不起作用.该属性根本没有做任何事情(我猜).少了什么东西?
我怎样才能实现它?任何提示?为什么Panel支持这个?
我正在使用wpf工具包AutoCompleteBox,我已经设置了Item模板.问题:弹出列表中的项目看起来很棒,但它对上面的文本框(选定项目)没有生效.

XAML:
<Controls:AutoCompleteBox x:Name="x" ItemFilter="SearchPerson" Margin="20">
<Controls:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<Rectangle Width="10" Height="10" Fill="LightGreen"/>
<TextBlock Text="{Binding Age}" />
</StackPanel>
</DataTemplate>
</Controls:AutoCompleteBox.ItemTemplate>
</Controls:AutoCompleteBox>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow() {
InitializeComponent();
Persons = new List<Person> {
new Person{Name = "Jhon",Age=35},
new Person{Name = "Kelly",Age=40}};
x.ItemsSource = Persons;
DataContext = this;
}
bool SearchPerson(string search, object value) {
return (value as Person).Name.ToLower().Contains(search);
}
}
public class Person …Run Code Online (Sandbox Code Playgroud) 我创建了简单的窗口和应用的样式.现在,当我运行应用程序背景为黑色时:

在XAML中它看起来很正常:

是什么造成的?
这是XAML代码:
<Window SizeToContent="WidthAndHeight"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WPFTest.MainWindow">
<Window.Style>
<Style TargetType="Window">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<Grid Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ContentPresenter />
</Grid>
<StatusBar Name="statusBar1" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Style>
<Grid>
<Grid Height="114" HorizontalAlignment="Center" Margin="1,0,557,622" Name="grid" VerticalAlignment="Bottom" Width="466">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25*" />
<RowDefinition Height="25*" />
<RowDefinition Height="25*" />
<RowDefinition Height="25*" />
</Grid.RowDefinitions>
<Label Content="Label4" Grid.Column="0" …Run Code Online (Sandbox Code Playgroud) 我想为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) 提供了设计时数据的DataContext是容易使用的d:DataContext,但是怎么样用引用控件属性{TemplateBinding}或{RelativeSource TemplatedParent}从Style.Template?
当我
(不能这样做,因为它会打破正常的设计经验).DesignerProperties.GetIsInDesignMode(this)返回true 时,我是否应该在构造函数/ Loaded事件中使用示例数据填充控件?
那些我无法修改的第三方控件呢?
编辑:当使用标准.NET ResourceDictionary时也会出现此问题,并且似乎是在控件或数据模板中使用资源字典的问题.
我有一个自定义资源字典遵循共享资源实例的常见方法.
http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ http://www.wpftutorial.net/MergedDictionaryPerformance.html
public class SharedResourceDictionary : ResourceDictionary
{
static readonly Dictionary<Uri, WeakReference<ResourceDictionary>> SharedDictionaries = new Dictionary<Uri, WeakReference<ResourceDictionary>>();
Uri _sourceUri;
public new Uri Source
{
get
{
// Behave like standard resource dictionary for IDE...
if (VisualStudio.IsInDesignMode)
return base.Source;
return this._sourceUri;
}
set
{
// Behave like standard resource dictionary for IDE...
if (VisualStudio.IsInDesignMode)
{
base.Source = value;
return;
}
this._sourceUri = value;
WeakReference<ResourceDictionary> cached;
if (SharedDictionaries.TryGetValue(value, out cached))
{
ResourceDictionary rd;
if (cached.TryGetTarget(out rd))
{
this.MergedDictionaries.Add(rd);
return; …Run Code Online (Sandbox Code Playgroud) 如何可靠地确定ControlTemplateWPF应用程序中是否使用了以下内容?文件名是'CheckBoxTemplates.xaml',并且与主应用程序位于不同的程序集中.注意,当我搜索文件名和资源键时没有结果.此外,在解决方案中搜索资源键是不可靠的.特别是,当有五个资源字典文件包含相同的密钥时.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna">
<Style x:Key="invertedCheckBox"
TargetType="CheckBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox"
...
Run Code Online (Sandbox Code Playgroud)
更多信息
user2250152应答者对于上述XAML是正确的.它无法可靠地确定是否正在使用样式.我这样说是因为当我将这种技术用于另一种风格时,我发现了五个包含相同密钥的资源字典文件.因此,我们必须考虑如何可靠地确定使用重复键定义的样式.
controltemplate ×10
wpf ×8
.net ×2
c# ×2
styles ×2
xaml ×2
asp.net ×1
combobox ×1
controls ×1
data-binding ×1
datatemplate ×1
design-time ×1
path ×1
stretch ×1
templates ×1