标签: templatebinding

使用TemplateBinding更新源代码

我将这种风格用于所有标签

    <Style TargetType="Label" x:Key="LabelStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <StackPanel Orientation="Horizontal"  >
                        <TextBox Loaded="MyTextBlock_Loaded" x:Name="EditControl" Visibility="Collapsed" Text="{TemplateBinding Tag}" />
                        <Label Content="{TemplateBinding Content}" Grid.Column="1" Grid.Row="1">
                        </Label>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

和我的样品标签

<Label Grid.Column="0" Grid.Row="0" Content="Photo" Style="{StaticResource LabelStyle}" Tag="{Binding fieldsCode.firstName, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)

但我觉得TemplateBiding不支持更新属性.怎么能解决这个问题

data-binding wpf templatebinding

8
推荐指数
2
解决办法
6801
查看次数

TemplateBinding如何在UserControl模板中工作?

我是新创建UserControl的人,现在我正在尝试自定义UserControl模板,如下所示:

<UserControl x:Class="WpfApplication1.PieButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Loaded="OnLoaded">
    <UserControl.Template>
        <ControlTemplate>
            <Path Name="path" Stroke="Aqua" StrokeThickness="3">
                <Path.Fill>
                    <SolidColorBrush Color="{TemplateBinding Fill}" />
                </Path.Fill>
                <Path.Data>
                ......
</UserControl>
Run Code Online (Sandbox Code Playgroud)

同时我在后端代码中创建了dependencyproperty:

public partial class PieButton : UserControl
{
    public PieButton()
    {
        InitializeComponent();
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {

    }



    public Color Fill
    {
        get { return (Color)GetValue(FillProperty); }
        set { SetValue(FillProperty, value); }
    }

    public static readonly DependencyProperty FillProperty =
        DependencyProperty.Register("Fill", typeof(Color), typeof(PieButton));
    ......
Run Code Online (Sandbox Code Playgroud)

我想TemplateBinding在XAML中使用绑定Fill我的PieButton的属性来填充路径对象.Visual Studio Designer警告我"无法访问或识别"填充属性".

根据我的理解, …

wpf xaml user-controls controltemplate templatebinding

8
推荐指数
1
解决办法
1万
查看次数

WPF:TemplateBinding到StrokeThickness的形状不起作用?

看起来ControlTemplate中的以下Ellipse没有获得BorderThickness,但为什么呢?

<Window.Resources>
    <ControlTemplate x:Key="EllipseControlTemplate" TargetType="{x:Type TextBox}">
        <Grid>
            <Ellipse 
                Width="{TemplateBinding ActualWidth}" 
                Height="{TemplateBinding ActualHeight}" 
                Stroke="{TemplateBinding Foreground}" 
                StrokeThickness="{TemplateBinding BorderThickness}" />
                <ScrollViewer Margin="0" x:Name="PART_ContentHost" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <TextBox
        Template="{DynamicResource EllipseControlTemplate}" 
        Foreground="Green"
        BorderThickness="15" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

TemplateBinding Foreground工作得很好,椭圆是绿色的.但StrokeThickness它似乎不起作用,为什么?

.net wpf xaml templatebinding .net-3.5

7
推荐指数
2
解决办法
2800
查看次数

WPF自定义控件:TemplateBinding到图像

我正在创建一个WPF自定义控件,一个ButtonImageText.我已经添加了两个依赖属性到控制,ImagePath并且Text,控制模板(在主题\ Generic.xaml)是水平排列的图像和文本的简单堆叠面板.

Text物业工作正常.但由于某些原因,在我的测试项目中的样本图像不,当我使用出现TemplateBindingImagePath依赖属性得到其路径.我已经通过临时替换TemplateBinding自定义控件中的图像路径来测试图像,在这种情况下它会出现.

我希望在这个领域有更多经验的人可以看看并告诉我为什么控制没有按预期工作.谢谢你的帮助.

我的VS 2008解决方案包含一个项目CustomControlDemo.该项目包含一个自定义控件,TaskButton.cs和一个主窗口Window1.xaml,我用它来测试控件.我的测试图像,calendar.png,坐落在该项目的根级资源文件夹,Generic.xaml坐落在一个主题文件夹中,也是在项目的根目录下.

这是我的自定义控件的代码(来自TaskButton.cs):

using System.Windows;
using System.Windows.Controls;

namespace CustomControlDemo
{
    public class TaskButton : RadioButton
    {
        #region Fields

        // Dependency property backing variables
        public static readonly DependencyProperty ImagePathProperty;
        public static readonly DependencyProperty TextProperty;

        #endregion

        #region Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        static TaskButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TaskButton), new FrameworkPropertyMetadata(typeof(TaskButton)));

            // Initialize ImagePath dependency properties
            ImagePathProperty = DependencyProperty.Register("ImagePath", typeof(string), …
Run Code Online (Sandbox Code Playgroud)

wpf image wpf-controls controltemplate templatebinding

7
推荐指数
2
解决办法
8696
查看次数

使用Converter的TemplateBinding - 出了什么问题?

我正在创建一个游戏桌.我想指定字段大小(一个字段是一个正方形)作为附加属性,并使用ViewPort的数据集值绘制2x2矩阵(并且平铺模式将完成游戏桌的其余部分).

我很遗憾出了什么问题,因为绑定不起作用.

在XAML中测试我想要的行为:

<DrawingBrush Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="None">
Run Code Online (Sandbox Code Playgroud)

游戏桌基于这个DrawingPaint示例:http://msdn.microsoft.com/en-us/library/aa970904.aspx(图像在这里)

XAML:

<Window x:Class="Sokoban.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Sokoban"
    Title="Window1" Height="559" Width="419">
    <Window.Resources>
        <local:FieldSizeToRectConverter x:Key="fieldSizeConverter" />
        <Style x:Key="GameDesk" TargetType="{x:Type Rectangle}">
            <Setter Property="local:GameDeskProperties.FieldSize" Value="50" />
            <Setter Property="Fill">
                <Setter.Value>
                    <!--<DrawingBrush Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="None">-->
                    <DrawingBrush Viewport="{TemplateBinding local:GameDeskProperties.FieldSize, Converter={StaticResource fieldSizeConverter}}" ViewportUnits="Absolute" TileMode="None">
                        <DrawingBrush.Drawing>
                            <DrawingGroup>
                                <GeometryDrawing Brush="CornflowerBlue">
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,100,100" />
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>

                                <GeometryDrawing Brush="Azure">
                                    <GeometryDrawing.Geometry>
                                        <GeometryGroup>
                                            <RectangleGeometry Rect="0,0,50,50" />
                                            <RectangleGeometry Rect="50,50,50,50" />
                                        </GeometryGroup>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                            </DrawingGroup>
                        </DrawingBrush.Drawing>
                    </DrawingBrush>
               </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <Rectangle Style="{StaticResource …
Run Code Online (Sandbox Code Playgroud)

wpf converter templatebinding

7
推荐指数
1
解决办法
9852
查看次数

TemplateBinding在某些情况下不起作用(使用TranslateTransform时)

这就是我在WPF中重现这个问题的方法:

创建自定义控件:

public class TestCustomControl : Control
{
static TestCustomControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl)));
}

public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}

// Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(TestCustomControl), new PropertyMetadata("Hello"));

public double OffSet
{
    get { return (double)GetValue(OffSetProperty); }
    set { SetValue(OffSetProperty, value); }
}

// Using a DependencyProperty as the backing store …
Run Code Online (Sandbox Code Playgroud)

silverlight wpf templatebinding windows-phone-7

7
推荐指数
1
解决办法
4841
查看次数

来自Style DataTrigger的TemplateBinding在ControlTemplate中

在下面的XAML中,我使用带有Border的Rectangle作为ToggleButton的模板.我希望BorderBrush是一个不同的颜色来反映ToggleButton.IsChecked的变化值.不幸的是,我在DataTrigger中使用TemplateBinding的尝试不起作用.我需要做什么呢?

<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <ControlTemplate x:Key="ButtonAsSwatchTemplate">
            <Border BorderThickness="1">
                <Border.Style>
                    <Style>
                        <Setter Property="BorderBrush" Value="Gainsboro" /> 
                        <Style.Triggers>
                            <!-- TemplateBinding doesn't work.-->
                            <DataTrigger                              
                                 Binding={TemplateBinding Property=IsChecked}    
                                 Value="True">
                                <Setter Property="BorderBrush" Value="Black" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <Rectangle Fill="{TemplateBinding Property=Background}"
                           Width="15" Height="15" />
            </Border>
        </ControlTemplate>
    </StackPanel.Resources>
    <ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}"
                  HorizontalAlignment="Center" VerticalAlignment="Center"
                  Margin="2" Background="Red" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

编辑

当我构建并重新加载设计器时,我收到以下错误:

错误1属性"绑定"不支持"TemplateBindingExpression"类型的值.

<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <ControlTemplate x:Key="ButtonAsSwatchTemplate">    
            <Border x:Name="SwatchBorder" BorderThickness="1">
                <Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" />
            </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter TargetName="SwatchBorder" Property="BorderBrush" Value="Yellow" />
            </Trigger>
        </ControlTemplate.Triggers>
            </ControlTemplate> …
Run Code Online (Sandbox Code Playgroud)

wpf datatrigger templatebinding control-template

6
推荐指数
1
解决办法
8250
查看次数

用模板绑定设置​​边框背景

Value ="{TemplateBinding HeaderColor}"我创建了自己的控件,我想知道是否可以将Border.Background绑定到模板属性.目前我正在使用如下的StaticResource设置它:

<Color x:Key="ControlMouseOverColor">green</Color>

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
    <EasingColorKeyFrame KeyTime="0:0:6" Value="{StaticResource ControlMouseOverColor}" />
</ColorAnimationUsingKeyFrames>
Run Code Online (Sandbox Code Playgroud)

我希望它是我控制的属性,并能够将其设置为模板绑定

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout">
    <EasingColorKeyFrame KeyTime="0:0:6" Value="{TemplateBinding HeaderColor}" />
</ColorAnimationUsingKeyFrames>
Run Code Online (Sandbox Code Playgroud)

MainPage.xaml中

<ctrl:Selection Grid.Column="0" HeaderColor="Red" HeaderText="Header Text" />
Run Code Online (Sandbox Code Playgroud)

我的课:

public static readonly DependencyProperty HeaderColorProperty =
        DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red));

public  System.Windows.Media.Color HeaderColor {
    get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); }
    set { SetValue(HeaderColorProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)

如果我能够这样做,第二个选项不起作用?我没有收到错误,它只是没有改变我设置的颜色.

AngelWPF留下的评论要求更多代码,粘贴在下面,我正处于学习创建控件的开始阶段,想要注意,因为有很多我还没有,一次一件:)

generic.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SelectionControl.Library"
xmlns:ctrl="clr-namespace:SelectionControl.Library;assembly=SelectionControl">

<LinearGradientBrush x:Key="HeaderBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="Black" Offset="0" />
    <GradientStop Color="Gray" Offset="1" />
</LinearGradientBrush>

<Color …
Run Code Online (Sandbox Code Playgroud)

silverlight background colors templatebinding

5
推荐指数
1
解决办法
3445
查看次数

在自定义控件上使用StringFormat绑定

我正在尝试在WPF应用程序中使用自定义控件,并且使用StringFormat绑定时遇到一些问题.

这个问题很容易重现.首先,让我们创建一个WPF应用程序并将其命名为"TemplateBindingTest".在那里,添加一个只有一个属性(Text)的自定义ViewModel,并将其分配给Window的DataContext.将Text属性设置为"Hello World!".

现在,向解决方案添加自定义控件.自定义控件尽可能简单:

using System.Windows;
using System.Windows.Controls;

namespace TemplateBindingTest
{
    public class CustomControl : Control
    {
        static CustomControl()
        {
            TextProperty = DependencyProperty.Register(
                "Text",
                typeof(object),
                typeof(CustomControl),
                new FrameworkPropertyMetadata(null));

            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
        }

        public static DependencyProperty TextProperty;

        public object Text
        {
            get
            {
                return this.GetValue(TextProperty);
            }

            set
            {
                SetValue(TextProperty, value);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将自定义控件添加到解决方案时,Visual Studio会自动创建一个带有generic.xaml文件的Themes文件夹.我们将控件的默认样式放在那里:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TemplateBindingTest">

    <Style TargetType="{x:Type local:CustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl}">
                    <TextBlock Text="{TemplateBinding Text}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

现在,只需将控件添加到窗口,并使用StringFormat在Text属性上设置绑定.还要添加一个简单的TextBlock以确保绑定语法正确:

<Window x:Class="TemplateBindingTest.MainWindow" …
Run Code Online (Sandbox Code Playgroud)

c# wpf string-formatting templatebinding

5
推荐指数
2
解决办法
4856
查看次数

TemplateBinding不适用于文本框文本

我有一个自定义控件EnhancedTextBox,它是一个具有a TextBox和a 的UserControl Button.对于消费者我希望它看起来大部分看起来像TextBox,所以我做了以下事情:

<UserControl.Template>
    <ControlTemplate TargetType="textBoxes:EnhancedTextBox">
    ...
    <TextBox Text="{TemplateBinding Text}"...
Run Code Online (Sandbox Code Playgroud)

在EnhancedTextBox我有

public static readonly DependencyProperty TextProperty =
  DependencyProperty.Register("Text", typeof (String), typeof (EnhancedTextBox));

public String Text
{
  get { return (String) GetValue(TextProperty); }
  set { SetValue(TextProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)

然而,当我使用它时如下:

<EnhancedTextBox Text="{Binding MyText, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}}" />
Run Code Online (Sandbox Code Playgroud)

然后,MyText永远不会更新,我检查EnhancedTextBox.Text,它是null.我错过了什么?我一直盯着这一点,无法弄清楚出了什么问题.我甚至认为可能是因为我使用的是同一个名字,所以创建一个叫做Text1无效的属性....

另外值得注意的是,如果我使用常规TextBox,那么这一切都有效.所以,我相当肯定问题在于EnhancedTextBox它自身

.net c# wpf dependency-properties templatebinding

5
推荐指数
1
解决办法
2736
查看次数