标签: controltemplate

如何访问位于ControlTemplate中的WPF控件?

通常,WPF控件在.xaml文件中声明,而不是在后面的代码中声明(.xaml.cs文件).但是,有时我需要在代码中使用其中的一些控件来操作它们.如果它"驻留"在xaml文件中,我如何获得这种控件的句柄?

wpf controls controltemplate

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

如何通过ControlTemplate中的Trigger设置DropShadowEffect的属性?

我有一个Button ControlTemplate和我试图修改DropShadowEffectBorder使用Trigger.这是我的Xaml:

<Button.Template>
  <ControlTemplate TargetType="Button">
    <Border x:Name="ButtonBorder" Margin="10" CornerRadius="5" Background="Gray">
      <Border.Effect>
        <DropShadowEffect ShadowDepth="5" x:Name="BorderEffect" />
      </Border.Effect>
      <ContentPresenter HorizontalAlignment="Center" />
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="Button.IsMouseOver" Value="True">
        <Setter Property="Background" TargetName="ButtonBorder" Value="LightGray" />
      </Trigger>
      <Trigger Property="Button.IsPressed" Value="True">
        <Setter Property="Margin" TargetName="ButtonBorder" Value="13,13,7,7" />
        <!-- this is where I get the error -->
        <Setter Property="ShadowDepth" TargetName="BorderEffect" Value="2" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Button.Template>
Run Code Online (Sandbox Code Playgroud)

我得到错误声明无法找到BorderEffect.
我也尝试过:

<Setter Property="Effect.ShadowDepth" TargetName="ButtonBorder" Value="2" />
Run Code Online (Sandbox Code Playgroud)

但我也得到一个错误,告诉我ShadowDepth在类型的对象上找不到属性Effect(因为它使用的是基类而不是DropShadowEffect)

我怎么解决这个问题?

wpf triggers controltemplate

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

为ItemsControl.ItemContainerStyle指定ControlTemplate

以下类似于我想要完成的任务.但是,我得到了错误

PropertyDescriptor值无效.

在模板上Setter.我怀疑这是因为我没有指定TargetTypeStyle; 但是,我不知道容器的类型ItemsControl.

<ItemsControl>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <TextBlock Text="Some Content Here" />
                            <ContentPresenter />
                            <Button Content="Edit" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <!-- heterogenous controls -->
    <ItemsControl.Items> 
        <Button Content="Content 1" />
        <TextBox Text="Content 2" />
        <Label Content="Content 3" />
    </ItemsControl.Items>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

wpf itemscontrol controltemplate itemcontainerstyle

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

访问ControlTemplate中的控件

这是xaml:

<Page.Resources>
    <ControlTemplate x:Key="WeddingButtonBigTemplate" TargetType="Button">
        <Grid>
            <Image x:Name="imgNormal" Source="../Images/Married_button2.png"/>
            <TextBlock x:Name="textBlock2" Style="{StaticResource RegularBlueSpecialBoldText}" LineHeight="28" LineStackingStrategy="BlockLineHeight" HorizontalAlignment="Center" Margin="10,30,10,70" TextWrapping="Wrap" TextAlignment="Center" VerticalAlignment="Stretch" >
                <Run FontSize="20" Text="The event of"></Run>
                <Run FontSize="28" Text="{DynamicResource strBride}"></Run>
            </TextBlock>
        </Grid>
    </ControlTemplate>
</Page.Resources>

<Grid HorizontalAlignment="Center" VerticalAlignment="Top" Width="1000">
    <Button x:Name="btnWedding" HorizontalAlignment="Left" Margin="10,20,0,-49" VerticalAlignment="Top" Template="{StaticResource WeddingButtonBigTemplate}" Foreground="#FF2B4072" Width="380" Click="btnClick" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

我想要访问名为的TextBlock textBlock2.
我试图覆盖OnApplyTemplate但是得到了null.

我试过了:

Grid gridInTemplate = (Grid)btnWedding.Template.FindName("grid", btnWedding);
var ct0 = btnWedding.Template.FindName("textBlock2", btnWedding);
var ct1 = btnWedding.FindName("textBlock2");
var ct2 = btnWedding.FindResource("textBlock2");
Run Code Online (Sandbox Code Playgroud)

gridInTemplate为null(从MSDN获取的样本).
当然,ct#都是null.

我在这里错过了什么?

c# wpf controltemplate c#-4.0

21
推荐指数
4
解决办法
2万
查看次数

控件模板中的模板绑定

我有以下控件模板.

我希望使用模板绑定在控件模板中设置图像控件的source属性.

但由于这是按钮控件的控件模板,而按钮控件没有source属性,因此在这种情况下我无法使用TemplateBinding.

<ControlTemplate x:Key="BtnTemplate" TargetType="Button">
        <Border CornerRadius="5"  Margin="15" Cursor="Hand">
            <StackPanel>
                <Image Name="Img" Style="{StaticResource ImageStyle}" Source="temp.jpg" Height="100" Width="100" Margin="5"></Image>
                <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
            </StackPanel>
        </Border>
    </ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

由于我必须为不同的按钮实例设置不同的图像,我也不能对路径进行硬编码.

请让我知道如何解决这种情况.

data-binding wpf xaml controltemplate templatebinding

20
推荐指数
2
解决办法
4万
查看次数

更改复选框大小WPF

我想在WPF中创建一个更大的复选框.

我发现我需要做一个控制模板,其中一个例子可以在这里找到:http: //msdn.microsoft.com/en-us/library/ms752319.aspx

如果我使用该代码,则复选框与默认外观不相似.我想要做的就是更改边框宽度和高度属性.

我需要一个看起来与默认值完全相同的控件模板,从那里我只需更改宽度和高度.有谁知道我在哪里找到一个?还是更好的方法?

wpf controltemplate

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

如何继承控制模板

我正在开发一个WPF项目,我已经过度CheckBox控制了一些特殊操作.这是正常的.

我的问题是ControlTemplate从主题中应用的(来自codeplex的shinyred.xaml)不适用于我的过度控制.有没有办法继承CheckBox ControlTemplate我的新控件使用?

我能找到的所有样本都专注于继承风格CheckBox,但没有关于ControlTemplate.

wpf wpf-controls controltemplate

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

Style.Triggers与ControlTemplate.Triggers

我什么时候应该选择Style.Triggers时,我应该选择ControlTemplate.Triggers?使用一个在另一个上有什么好处吗?

假设我有这些样式可以达到相同的效果:

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ControlTemplate.Triggers>
                    ...
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        ...
    </Setter>
    <Style.Triggers>
        ...
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml styles controltemplate

18
推荐指数
1
解决办法
9649
查看次数

以编程方式访问ResourceDictionary项

我有一个名为"MySilverlightControls"的Silverlight控件组件.几个文件夹进入该程序集我有一个类,它从第三方供应商扩展一个网格列,我们称之为"MyImageColumn.cs".

我还创建了一个名为的资源字典Generic.xaml,它位于Themes程序集的文件夹中.在那个资源字典中,我定义了一个名为MyImageColumnTemplate的ControlTemplate :

<ControlTemplate x:Name="MyImageColumnTemplate" >
    <Grid Margin="8,8,4,4" MaxHeight="32" MaxWidth="32">
        <Grid.Resources>
            <localGrid:StatusColumnImageConverter x:Key="ImageContentConverter"/>
        </Grid.Resources>
        <Border Margin="5,5,0,0" Background="Black" Opacity="0.15" CornerRadius="5" />
        <Border Background="#FF6E6E6E" CornerRadius="4,4,4,4" Padding="4" Margin="0,0,5,5">
            <Border Background="White" CornerRadius="2,2,2,2" Padding="3">
                <Image Source="{Binding EditValue, Converter={StaticResource ImageContentConverter}}" Stretch="Uniform"/>
            </Border>
        </Border>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我的问题是:从MyImageColumn,我如何以编程方式引用/加载此控件模板,以便将其分配给列上的属性?我希望使用类似这样的语法:

ControlTemplate ct = (ControlTemplate)Application.Current.Resources["MyImageColumnTemplate"];
Run Code Online (Sandbox Code Playgroud)

但是这总是返回null.当我在Reflector中加载程序集时,我看到Generic.xaml文件在那里,资源的名称是MySilverlightControls.g.resources,并且其中的路径是themes/generic.xaml.

我怎样才能获得此资源字典中的各个项目?

silverlight resources resourcedictionary controltemplate silverlight-3.0

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

WPF:使用TemplateBinding将整数绑定到TextBlock

我在WPF中有一个自定义控件.在这我有一个DependencyProperty类型int.在自定义控件的模板中我有一个TextBlock,我想在中显示整数的值TextBlock.但我无法让它发挥作用.

我正在使用TemplateBinding.如果我使用相同的代码,但是改变的类型DependencyProperty,以string正常工作.但我真的希望它是我的应用程序其余部分工作的整数.

我怎样才能做到这一点?

我编写了简化的代码来显示问题.首先是自定义控件:

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

        MyIntegerProperty = DependencyProperty.Register("MyInteger", typeof(int), typeof(MyCustomControl), new FrameworkPropertyMetadata(0));
    }


    public int MyInteger
    {
        get
        {
            return (int)GetValue(MyCustomControl.MyIntegerProperty);
        }
        set
        {
            SetValue(MyCustomControl.MyIntegerProperty, value);
        }
    }
    public static readonly DependencyProperty MyIntegerProperty;
}
Run Code Online (Sandbox Code Playgroud)

这是我的默认模板:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border BorderThickness="1" CornerRadius="4" BorderBrush="Black" Background="Azure">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{TemplateBinding MyInteger}" HorizontalAlignment="Center" …
Run Code Online (Sandbox Code Playgroud)

wpf xaml binding controltemplate

15
推荐指数
1
解决办法
6759
查看次数