标签: controltemplate

DataContext作为资源中转换器绑定的源

 <Canvas.DataContext>
  <ViewModels:VMSomeControl Model="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
 </Canvas.DataContext>

 <!-- DataContext is not passed into these Instances.
      they also have no knowledge of their TemplatedParent. -->
 <Canvas.Resources>

  <!--  is there a way to use a binding that points to the datacontext within the resources ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{Binding Path=Model.SomeProperty}" />

  <!--  is there a way to point Directly to the TemplatedParent  ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{TemplateBinding SomeProperty}" />

 </Canvas.Resources>


 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=0}" />

 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, …
Run Code Online (Sandbox Code Playgroud)

wpf datacontext binding controltemplate ivalueconverter

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

对 ControlTemplate 内控件的引用

我如何在代码隐藏中形成我的构造函数以获取对下面 XAML 中 OuterBorder 控件的引用?

<Window Template="{DynamicResource WindowTemplate}">
    <Window.Resources>      
        <ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
            <AdornerDecorator>
                <Border Name="OuterBorder" Background="Black" BorderBrush="Red" BorderThickness="1" CornerRadius="0">
                    <!-- Implementation here... -->
                </Border>
            </AdornerDecorator>
        </ControlTemplate>
    </Window.Resources>
</Window>
Run Code Online (Sandbox Code Playgroud)

c# wpf resources controltemplate

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

WPF自定义控件模板未应用

我确信这个问题或它的衍生物已被问过无数次,但我找不到任何帮助我解决问题的东西,所以我问.请随时告诉我我确定存在但我找不到的副本.显然我对关键词的关注度不高.

我有一个自定义控件,它有自己的资源字典,仅用于定义控件模板.然后将该字典合并到Generic.xaml中.

问题是当这个控件出现在UI中时,它内部没有任何内容.我用Snoop找到了这个.控件位于UI中,但它完全为空.

您将在下面找到我认为对此问题负责的项目.我们非常感谢您提供的任何帮助或建议.

我的文件夹结构的相关部分是这样的:

我的目录结构

BasicTemplate.xaml:

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

    <Style TargetType="{x:Type local:BasicTemplate}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BasicTemplate}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock Text="This is a basic template." />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Layouts/Templates/XAML/BasicTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

wpf custom-controls resourcedictionary controltemplate

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

使用WrapPanel为ListView.GroupStyle设置样式

好的,这是我非常简单的问题.

我有一个ListView我设计的样式,使它看起来像Windows资源管理器.

现在,我想将内容分组.因此,我定义了GroupStyle一个Expander用于对它进行分组.分组现在很好.

我不喜欢的是,现在,我将ListView每个组显示在一个单独的行上,而我希望有一些扩展器包装,以便在同一行上显示多个组.

我认为图像比某些文字更好.

这是我有的:

是)我有的

这就是我想要的:

我想要的是

我找不到要设置样式的属性,以便GroupItems在WrapPanel中进行调整,就像我对项目所做的那样.

这是我的ListView样式:

<ResourceDictionary>

                    <!-- Explorer-style layout-->
                    <DataTemplate x:Key="ExplorerView">
                        <StackPanel Orientation="Horizontal" Height="Auto" Width="150">
                            <Image Source="{Binding Path=Value.AppConfig.Appli.AppType, Converter={StaticResource TypeToIconConverter}}" Margin="5"
                                   Height="50" Width="50"/>
                            <StackPanel VerticalAlignment="Center" Width="90">
                                <TextBlock Text="{Binding Path=Value.AppConfig.Appli.AppName}" 
                     FontSize="13" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow"
                     Margin="0,0,0,1" />
                                <TextBlock Text="{Binding Path=Value.AppConfig.Appli.AppType}" FontSize="9" 
                     HorizontalAlignment="Left" Margin="0,0,0,1" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>

                    <!-- Group header style-->
    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">


        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">

                    <Expander x:Name="exp" IsExpanded="True" Width="310"
                                   BorderBrush="CornflowerBlue">

                        <Expander.Header>
                            <DockPanel HorizontalAlignment="Stretch" …
Run Code Online (Sandbox Code Playgroud)

wpf xaml listview styles controltemplate

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

WPF 验证控制模板重叠

我有一个带有控件模板的用户控件来显示验证错误,验证模板:

<ControlTemplate x:Key="TextBoxPropertyValidationTemplate">
        <StackPanel>
            <Border BorderBrush="Red" BorderThickness="1">
                <AdornedElementPlaceholder x:Name="MyAdorner" />
            </Border>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" MaxHeight="16" MaxWidth="16"
                       Source="{Binding Source={StaticResource ValidationIcon}, Converter={StaticResource UriConverter}}" 
                       Margin="1" RenderOptions.BitmapScalingMode="HighQuality"
                       VerticalAlignment="Center" HorizontalAlignment="Center" />
                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left"
                           Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                           TextWrapping="Wrap" Grid.Column="1" FontSize="10" Foreground="Red" />
            </Grid>

        </StackPanel>
    </ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

而且我似乎无法解决一个看起来像这样的令人恼火的问题:

验证

我一直在尝试使用用户控件和模板上的边距,还有一些 Height=Auto 等,但所有这些都没有真正的帮助。任何人的想法?

如果这有助于主用户控件(嵌套带有验证的控件)位于带有 AdornerDecorator 的 TabItem 中。

任何帮助表示赞赏。

validation wpf adorner controltemplate

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

正确设置 DataGridCell 样式

这是我之前的问题之后的一个问题,你可以在那里找到它

所以。现在我为每一列定义了DataGrid一个特定ElementStyle的(它只是TextBlock用粗体和白色定义了里面的s - 稍后会解决这个问题)

所以现在我有两个问题

第一个问题(已解决)

当我为我的单元格设置背景时,它会覆盖默认样式,并且当单元格突出显示时背景保持不变。

一种风格的例子:

<!-- Green template for market-related -->
<ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}">
    <Grid Background="Green">
        <ContentPresenter
                        HorizontalAlignment="Center"
                                  VerticalAlignment="Center" />
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我自然会说这是“正常的”,因为我将Grid背景设置为绿色。因此,我以这种方式尝试过:

<!-- Light green template for sophis-related -->
<ControlTemplate x:Key="LightGreen" TargetType="{x:Type tk:DataGridCell}">
    <Grid Background="LightGreen">
        <Grid.Resources>
            <Style TargetType="{x:Type Grid}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type tk:DataGridCell}}, 
                                            Converter={StaticResource DebugConverter}}" Value="True">
                        <Setter Property="Grid.Background" Value="#FF3774FF" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

这也行不通。正如你所看到的,我放置了一个 DebugConverter …

wpf datagrid styles controltemplate

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

将ControlTemplate基于另一个ControlTemplate

我知道必须有一个简单的答案,但我找不到它.

我有一个叫做按钮的样式HoverButton.

<Style x:Key="HoverButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="Background" Value="WhiteSmoke"/>
                        <Setter Property="Foreground" Value="DarkRed"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Background" Value="Transparent"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

然后,我想创建另一个基于HoverButton特定内容的"派生"样式.我在这里减少了上述风格的复杂性,但它足够复杂,我不想复制和粘贴它.

<Style x:Key="CloseButton" BasedOn="{StaticResource HoverButton}" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
            <Path Width="8" Height="8" Stroke="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}" Data="M0,0 L8,8 M8,0 L0,8" StrokeThickness="2" />
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这不起作用 - "指定的元素已经是另一个元素的逻辑子元素.首先断开它." 看起来我需要重新定义Template派生样式的属性,但不知何故引用了基本样式的模板.

有任何想法吗?

wpf xaml styles controltemplate

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

带圆角的数据网格模板

我正在创建带有圆角的 DataGrid 模板,但我遇到了这个问题: 在此处输入图片说明

底角超出边界边界。如何解决这个问题?我读过这篇文章:http : //diptimayapatra.wordpress.com/2010/06/28/rounded-corner-datagrid-in-wpf/

我需要做同样的事情,但在模板中。这是我的 DataGrid 模板代码:

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Background" Value="{StaticResource ControlContentBrush}"/>
    <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Margin" Value="5"/>
    <Setter Property="AlternatingRowBackground" Value="#282828"/>
    <Setter Property="HorizontalGridLinesBrush" Value="{StaticResource NormalBorderBrush}"/>
    <Setter Property="VerticalGridLinesBrush" Value="{StaticResource NormalBorderBrush}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border x:Name="Border" BorderBrush="#282828" BorderThickness="2" CornerRadius="13" Background="#232323" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf controltemplate wpfdatagrid

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

触发MouseOver状态后,为什么ColorAnimation for Checked状态不会保持颜色?

我遇到的问题与ControlTemplateToggleButton我创造.

当按钮为时Checked,ColorAnimation触发a并且控件的背景改变颜色.但是,如果用户进入MouseOver状态,则会触发另一个影响按钮背景的动画.

当鼠标不再处于该MouseOver状态时,控件不会返回到处于该Checked状态时应该处于的颜色.我不确定为什么在MouseOver触发状态时这种情况不会持续存在.

VisualStateManager我的部分ControlTemplate看起来有点儿像这样:

<VisualStateManger.VisualStateGroups>
    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal"></VisualState>
        <VisualState x:Name="MouseOver">
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="BackgroundBorder"
                    Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                    To="Gold" Duration="0:0:0.3" />
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
    <VisualStateGroup x:Name="CheckedStates">
        <VisualState x:Name="Checked">
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="BackgroundBorder"
                    Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                    To="PaleGoldenrod" Duration="0:0:0.3" />
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
Run Code Online (Sandbox Code Playgroud)

wpf controltemplate togglebutton visualstates

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

WPF - 单选按钮控件模板绑定"IsChecked"不起作用

我有一个如下控件模板,我想IsChecked在用户选择单选按钮时获取属性.

但是当用户选择单选按钮"A"时,它的IsChecked属性仍然显示为false.为什么?

<ControlTemplate x:Key="RadioBtnTemplate" TargetType="{x:Type RadioButton}">
   <Grid>
     <StackPanel Margin="5">
         <RadioButton Name="tempbtn" IsChecked="{TemplateBinding IsChecked}" FontFamily="Segoe UI" FontSize="18.667" Content="{TemplateBinding Content}" GroupName="{TemplateBinding GroupName}"/>
      </StackPanel>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我使用这个模板:

<RadioButton GroupName="CG" x:Name="_rdoBtnA" Content="A" Template="{DynamicResource RadioBtnTemplate}" IsChecked="True"/>
<RadioButton GroupName="CG" x:Name="_rdoBtnB" Content="B" Template="{DynamicResource RadioBtnTemplate}" />
<RadioButton GroupName="CG" x:Name="_rdoBtnC" Content="C" Template="{DynamicResource RadioBtnTemplate}" />
Run Code Online (Sandbox Code Playgroud)

wpf xaml controltemplate ischecked

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