标签: controltemplate

ComboBox的自定义模板忽略DisplayMemberPath的使用

所以我在网站上经历了几个问题,我似乎无法找到答案.

我有一个ComboBox.它运作得很好.我决定需要彻底检查外观,所以我创建了默认ComboBox模板的副本(这是一个直接副本,没有修改):

<ControlTemplate x:Key="ComboBoxControlTemplate2" TargetType="{x:Type ComboBox}">
            <Grid x:Name="MainGrid" SnapsToDevicePixels="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                </Grid.ColumnDefinitions>
                <Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                    <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
                        <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                            <ScrollViewer x:Name="DropDownScrollViewer">
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                                    </Canvas>
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Microsoft_Windows_Themes:SystemDropShadowChrome> …
Run Code Online (Sandbox Code Playgroud)

wpf xaml combobox controltemplate

11
推荐指数
1
解决办法
5276
查看次数

ControlTmplate与DataTrigger相比.DataTemplate与DataTemplateSelector

我有一个通用控件,它根据ViewModel中的type属性显示一个编辑器.目前它已经实现使用Control,ControlTemplate并且DataTrigger像这样 -

<Control
   x:Name="MainControl"
   Grid.Column="1"
   TargetUpdated="OnTargetUpdated">
        <Control.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger
                        Binding="{Binding Path=EditorType}"
                        Value="{x:Static view:EditorType.Bool}">
                        <Setter
                            Property="Control.Template"
                            Value="{StaticResource boolTemplate}" />
                    </DataTrigger>
                    <DataTrigger
                        Binding="{Binding Path=EditorType}"
                        Value="{x:Static view:EditorType.Text}">
                        <Setter
                            Property="Control.Template"
                            Value="{StaticResource textTemplate}" />
                    </DataTrigger>
                    <DataTrigger
                        Binding="{Binding Path=EditorType}"
                        Value="{x:Static view:EditorType.Integer}">
                        <Setter
                            Property="Control.Template"
                            Value="{StaticResource integerTemplate}" />
                    </DataTrigger>
                    ...
                    ....
                </Style.Triggers>
            </Style>
     </Control.Style>
</Control>
Run Code Online (Sandbox Code Playgroud)

现在,同样可以利用来实现ContentPresenter,DataTemplateDataTemplateSelector这样的-

<local:EditorTemplateSelector
    BoolEditorTemplate="{StaticResource boolTemplate}"
    TextEditorTemplate="{StaticResource textTemplate}"
    IntegerEditorTemplate="{StaticResource integerTemplate}"
    ...
    ....
    x:Key="EditorTemplateSelector">
</local:EditorTemplateSelector>

<ContentPresenter
    ContentTemplateSelector="{Binding Source={StaticResource EditorTemplateSelector}}"
    Content="{Binding}"
    TargetUpdated="OnTargetUpdated">
</ContentPresenter>

// …
Run Code Online (Sandbox Code Playgroud)

wpf datatrigger datatemplate controltemplate datatemplateselector

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

WPF ControlTemplate打破了风格

有效的东西

我需要设置某种类型的控件的样式,这些控件是StackPanel的子代.我正在使用:

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">...</Style>
    </StackPanel.Resources>
    <TextBlock ...>
    ...
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

这很好用!每个TextBlock都会查看它的父级(StackPanel)的资源,以了解它应该如何设置样式.将TextBlock嵌套到StackPanel上的距离无关紧要...如果它在其直接父级中找不到样式,它将查看其父级的父级,依此类推,直到找到某些东西(在这种情况下) ,中所定义的风格).

那些不起作用的东西

当我在ContentControl中嵌套TextBlock时遇到问题,ContentControl有一个模板(参见下面的代码).ControlTemplate似乎破坏了TextBlock从其父母,祖父母那里检索其风格的方式......

有效地使用ControlTemplate似乎可以解决TextBlock找到其合法风格的方法(StackPanel.Resources中的风格).遇到ControlTemplate时,它会停止在树上的资源中查找其样式,而是默认使用Application本身的MergedDictionaries中的样式.

<StackPanel Orientation="Vertical" Background="LightGray">
    <StackPanel.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Green" />
        </Style>
    </StackPanel.Resources>

    <TextBlock Text="plain and simple in stackpanel, green" />
    <ContentControl>
        <TextBlock Text="inside ContentControl, still green" />
    </ContentControl>
    <ContentControl>
        <ContentControl.Template>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                <StackPanel Orientation="Vertical">
                    <ContentPresenter />
                    <TextBlock Text="how come this one - placed in the template - is not green?" />
                </StackPanel>
            </ControlTemplate>
        </ContentControl.Template>
        <TextBlock Text="inside ContentControl with a template, this …
Run Code Online (Sandbox Code Playgroud)

wpf xaml nested controltemplate

11
推荐指数
1
解决办法
3805
查看次数

WPF主题可以用于包含可在运行时更改的应用程序的多个外观吗?

WPF允许控件库为不同的系统主题提供不同的资源字典,基本上允许应用程序匹配操作系统选择的视觉主题(Aero,Luna等).

我想知道我是否可以在我的应用程序中包含多个主题资源字典,并在框架中使用一些现有的主题支持.这应该适用于我自己的主题名称,理想情况下允许用户在运行时更改主题,从而更改应用程序的外观.即使这只是配置设置,它仍然可能很有趣.

wpf skins themes styles controltemplate

10
推荐指数
1
解决办法
3409
查看次数

WPF Border DesiredHeight

以下Microsoft示例代码包含以下内容:

<Grid>
...     
  <Border Name="Content" ... >
...     
  </Border>
</Grid>
<ControlTemplate.Triggers>
  <Trigger Property="IsExpanded" Value="True">
     <Setter TargetName="ContentRow" Property="Height"
             Value="{Binding ElementName=Content,Path=DesiredHeight}" />
  </Trigger>
...
</ControlTemplate.Triggers>
Run Code Online (Sandbox Code Playgroud)

但是,运行时,此代码会生成以下数据绑定错误:

System.Windows.Data Error: 39 : BindingExpression path error: 'DesiredHeight' property not found on 'object' ''Border' (Name='Content')'. BindingExpression:Path=DesiredHeight; DataItem='Border' (Name='Content'); target element is 'RowDefinition' (HashCode=2034711); target property is 'Height' (type 'GridLength')
Run Code Online (Sandbox Code Playgroud)

尽管有这个错误,代码仍能正常工作.我查看了文档并且DesiredHeight似乎不是其成员Border.任何人都可以解释DesiredHeight来自哪里?另外,有没有办法解决/抑制此错误,所以我的程序输出是干净的?

data-binding wpf controltemplate

10
推荐指数
2
解决办法
3563
查看次数

WPF - 在样式中使用ControlTemplate资源

创建样式时,是否可以将ControlTemplate属性设置为先前定义的资源?例如,如果我在ResourceDictionary中有以下内容:

<ControlTemplate x:Key="MyControlTemplate" TargetType="{x:Type Button}">
...
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

后来想在这样的样式中使用它:

<Style x:Key="MyStyle" TargetType="{x:Type Button}">
    <Setter Property="Template" Value="???"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

那可能吗?

wpf xaml styles controltemplate

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

具有相对尺寸的RectangleGeometry ......怎么样?

我正试图在我正在创建的按钮的控制模板上复制如今时尚的"反射"效果.

基本思想是创建一个矩形,其渐变填充从白色到透明,然后用矩形几何体剪切一些半透明矩形.

问题是我不知道如何定义相对矩形几何.我通过定义一个大值(1000)来解决宽度问题,但高度是一个问题.例如,它适用于高度为200的按钮,但对于较小的按钮不起作用.

有任何想法吗?

            <Rectangle RadiusX="5" RadiusY="5" StrokeThickness="1" Stroke="Transparent">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,0.55">
                        <GradientStop Color="#66ffffff" Offset="0.0"  />
                        <GradientStop Color="Transparent" Offset="1.0" />
                    </LinearGradientBrush>
                </Rectangle.Fill>
                <Rectangle.Clip>
                    <RectangleGeometry Rect="0,0,1000,60" />
                </Rectangle.Clip>
            </Rectangle>
Run Code Online (Sandbox Code Playgroud)

wpf controls clipping controltemplate

10
推荐指数
1
解决办法
8237
查看次数

WPF ControlTemplates必须有TargetType吗?

WPF中的ControlTemplates是否需要TargetType?我正在重新设置一些控件,并注意到comboboxitem,listiviewitem和listboxitem都具有相同的模板:

    <ControlTemplate x:Key="ListBoxItemCT" TargetType="{x:Type ListBoxItem}">

    <Border x:Name="Bd" 
        SnapsToDevicePixels="true" 
        Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        Padding="{TemplateBinding Padding}"
        CornerRadius="1">
        <ContentPresenter x:Name="cpItemContent"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
            />
    </Border>

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

是否可以删除TargetType并为所有三个模板设置一个模板?我试图这样做,但得到奇怪的错误和问题.我找不到ControlTemplates必须具有类型的任何特定参考.

.net wpf templates controltemplate targettype

10
推荐指数
1
解决办法
3669
查看次数

从WPF中的代码设置验证错误模板

我的WPF应用程序中有一个TextBox.我已经为验证错误定义了一个ControlTemplate,如下所示:

<ControlTemplate x:Key="validationTemplate">
    <DockPanel LastChildFill="True">
         <TextBlock DockPanel.Dock="Bottom"  Text="Invalid Input: "></TextBlock>
                 <AdornedElementPlaceholder />
    </DockPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我的TextBox如下:

<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}">                                              
    <TextBox.Text>
        <Binding Path="TEXT1" ValidatesOnDataErrors="True" validatesOnExceptions="True">
         </Binding>
    </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

现在,如果我的TextBox添加了ValidationRule,然后我在那里验证,错误模板正确应用.但由于其他一些问题我无法做到这一点.

所以我必须在PreviewLostKeyboardFocus中验证TextBox的内容.我正在验证TextBox.现在我想在后面的代码中为TextBox设置错误模板,但我无法做到!!

我尝试了这个,但它并不像有意的那样工作::

private void blockTextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        TextBox txtBox = sender as TextBox;
        txtBox.Template = this.FindResource("validationTemplate") as ControlTemplate;

        //this behaves strange; it removes the TextBox and places the ErrorTemplate. 
       //I want it to behave like the way WPF does internally wherein it places 
       //the error template around TExtBox
    }
Run Code Online (Sandbox Code Playgroud)

问题1:我想知道如何将错误模板添加到TextBox

问题2:我想知道如何从代码中设置控件模板的错误消息.例如,我想将默认错误消息"Invalid …

validation wpf textbox code-behind controltemplate

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

如何更改MahApps.Metro对话框内容模板的宽度?

我想更改MahApps.Metro对话框的基本模板(或创建一个新的对话框类型),因为我想在一个狭窄的登录窗口中显示它们.现在几乎消息中的所有第二个单词都在一个新行中,但右侧和左侧有很大的空格,我想减少它们.

两侧的衬垫太宽

我发现在BaseMetroDialog.xaml该消息对话框分为垂直三个部分:25%的左侧空间,50%的内容和25%的空间在右侧.我想改变这些数字.

但是我怎么能用我的BaseMetroWindow新模板改变控制模板呢?

wpf xaml modal-dialog controltemplate mahapps.metro

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