标签: controltemplate

XAML 中控制模板中元素的访问属性

我想使用由图像和标签组成的模板化 ComboBoxItems。如果我将模板分配给 ComboBoxItem,我可以以某种方式设置图像的源属性吗?目标是为不同的 ComboBoxItems 使用相同的模板,但在每个 Item 中使用不同的图片。

我还考虑过在模板中绑定 Image.Source-Property,但这失败了,因为“父”ComboBoxItem 当然没有我可以绑定的 Source-Property。

代码说明了我的问题:

    <Style x:Key="ComboBoxPictureItem" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <Image x:Name="StatusImage" />
                        <Label x:Name="StatusLabel" Content="Green"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ComboBox>
        <ComboBoxItem Style="{StaticResource ResourceKey=ComboBoxPictureItem}"
-> sth. like:         StatusImage.Source="PathToMyImage.png"/>
    </ComboBox>
Run Code Online (Sandbox Code Playgroud)

谢谢!

wpf xaml wpf-controls controltemplate

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

如何更改 WPF CheckBox 控件中 CheckMark 的颜色?

我有一个自定义模板复选框,我正在使用视图框(使用视图框允许简单缩放)实现它,我无法弄清楚如何更改选中/取消选中复选框时显示的内容。

我希望选中时复选标记为红色(不是最终外观,只是想看看它是否有效)。

我的复选框样式:

<Style x:Key="KioskCheckBox" TargetType="{x:Type CheckBox}">
        <Setter Property="FontFamily" Value="{StaticResource FontFamilyLight}" />
        <Setter Property="FontSize" Value="{StaticResource KioskNormalFontSize}" />
        <Setter Property="Foreground" Value="{StaticResource brshSystemTextColor}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">

                        <!--<Viewbox HorizontalAlignment="Left" 
                                 VerticalAlignment="Top"
                                 Stretch="Fill" 
                                 Height="30"   
                                 Width="30" Margin="10,0,0,0">
                            <Grid Height="200" Width="200">
                                <Ellipse  
                                Fill="Transparent"
                                StrokeThickness="15" 
                                Stroke="{StaticResource brshSystemTextColor}"/>
                                <Path 
                                Stroke="{StaticResource brshSecondaryColor}"
                                Fill="Transparent" 
                                Stretch="None"
                                StrokeThickness="20"
                                Data="M 30,100 L 80,140 L 160,60" Margin="0,0,2,2"/>
                            </Grid>
                        </Viewbox>-->
                        <ContentControl>
                        <StackPanel Orientation="Horizontal">
                            <Viewbox HorizontalAlignment="Left" 
                                VerticalAlignment="Center"
                                Stretch="Fill" 
                                Height="30"   
                                Width="30" Margin="10,0,0,0">
                                <Grid Height="200" Width="200">
                                    <Ellipse  
                                        Fill="Transparent"
                                        StrokeThickness="15" 
                                        Stroke="{StaticResource brshSystemTextColor}"/>
                                    <Path 
                                        Stroke="{StaticResource brshSecondaryColor}"
                                        Fill="Transparent" 
                                        Stretch="None" …
Run Code Online (Sandbox Code Playgroud)

wpf controltemplate

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

在 WPF/Silverlight 中将 ComboBox 作为 TextBlock 呈现

我想将 ComboBox 显示为 TextBox(没有边框、背景、切换按钮等) - 仅当前选定的项目文本。我喜欢这个,但我不明白如何链接 TextBlock,以便它在 ComboBox 中显示当前选定的项目。

<ComboBox ItemsSource="{Binding Path=...}" SelectedValue="{Binding Path=...}" DisplayMemberPath="Name" SelectedValuePath="Id">
    <ComboBox.Template>
        <ControlTemplate>
            <TextBlock Text="{Binding ?}"></TextBlock>
        </ControlTemplate>
    </ComboBox.Template>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

wpf combobox controltemplate

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

如何在 WPF TreeView 中突出显示整行

我想调整默认的 WPF TreeView/TreeViewItem 模板,以便可以突出显示整行,如图所示:

在此处输入图片说明

但是,我尝试了谷歌搜索中的几个模板,但没有一个可以真正产生效果,一些错误的答案甚至被标记为正确......

代码流中有一个答案似乎有效,但它添加了额外的 C# 代码,或者不需要额外的代码但工作不完美。

我不想添加额外的 C# 代码,而只想更改默认模板。有人有什么好主意吗?

非常感谢!

- - 编辑 - -

@Nick,使用您的模板后,显示如下,

首先,它没有突出显示“整个”行,“整个”是指树的最宽宽度。

其次,它突出了包括儿童在内的其他领域。

在此处输入图片说明

wpf treeview controltemplate treeviewitem

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

避免每次在 ApplyTemplate() 时加载 XAML

我正在研究 WPF 网格的性能改进,并发现ApplyTemplate()对单元格的调用消耗了相当多的资源。每次我们创建一个单元格并将其应用ControlTemplate到它时,它会再次加载 XAML,尽管我认为这应该为同一个模板缓存。

分析结果

这样可以节省一次又一次加载 XMAL 的成本吗?

另外,是否可以构建一个ControlTemplate不依赖于 XAML 的,但我们可以通过编程方式设置可视化树来提高性能?

更新:

我尝试使用由代码创建的模板并完全由FrameworkElementFactory. 分析结果表明我们没有通过这种方法获得性能提升。

使用 FrameworkElementFactory 分析结果

似乎每次ApplyTemplate调用中的 XAML 解析成本都不高,但可视化树实例化花费了大部分时间。

wpf xaml controltemplate

5
推荐指数
0
解决办法
487
查看次数

如何更改 MahApps.Metro 对话框样式?

我想更改消息对话框的样式。我用过这个:如何更改 MahApps.Metro 对话框内容模板宽度?

我想要一些对话有不同的风格。如何更改单个对话框的样式?我尝试用财产来做这件事CustomResourceDictionary。但这没有效果。

var Style = (Style) Application.Current.Resources["NewCustomDialogStyle"];
var mySettings = new MetroDialogSettings()
    {
        CustomResourceDictionary = Style.Resources
    };
Run Code Online (Sandbox Code Playgroud)

当我覆盖 MessageDialog 样式时,一切都很好

<Style TargetType="{x:Type Dialog:MessageDialog}" BasedOn="{StaticResource NewCustomDialogStyle}" />
Run Code Online (Sandbox Code Playgroud)

wpf xaml modal-dialog controltemplate mahapps.metro

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

更改 WPF DatePicker 文本框的外观

更改 WPF DatePicker 的 Foreground 属性确实会更改文本框中文本的颜色,但更改 Background 属性不会。但是您可以通过设置包含的 DatePickerTextBox 的样式来更改它。所以我最终得到了这个:

<DatePicker Foreground="Yellow" >
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}" >
            <Setter Property="Background" Value="Black" />
        </Style>
    </DatePicker.Resources>
</DatePicker>
Run Code Online (Sandbox Code Playgroud)

有没有更整洁的方法来做到这一点而不对整个控件进行模板化?有没有办法只对命名部分进行模板化,即 PART_TextBox?

c# wpf datepicker controltemplate

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

将自定义依赖项属性添加到XAML中的控制模板

经过一段时间的休息后,我已经设法进一步使用了我的只读复选框,现在我以理想的优雅形式拥有了我想要的功能.问题是我已经使用了一些黑客来使它工作,虽然这不是一个灾难,它会更好地做到这一点.

回顾一下:我想要一个常规查看复选框,在单击它时不会自我检查,而是单击事件会触发后台工作程序,稍后会导致更新变量.此变量绑定到checkbox.ischecked,然后使用新值更新.

我想在这里使用基于这个想法的控件模板:

C#WPF中的只读CheckBox

我修改了这个并剥离了我认为我不需要的东西(也许是不明智的)并最终得到:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<!-- -->
<Style x:Key="ReadOnlyCheckBoxStyle" TargetType="{x:Type CheckBox}" >
        <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <BulletDecorator SnapsToDevicePixels="true" Background="Transparent">
                    <BulletDecorator.Bullet>
                        <Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}"
                                                               BorderBrush="{TemplateBinding BorderBrush}"
                                                               RenderMouseOver="{TemplateBinding IsMouseOver}"
                                                               IsChecked="{TemplateBinding Tag}">
                        </Microsoft_Windows_Themes:BulletChrome>
                    </BulletDecorator.Bullet>
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      Margin="{TemplateBinding Padding}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="True" />
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这个复选框的工作方式如上所述,我这样称呼它:

<CheckBox x:Name="uiComboBox" Content="Does not set the backing property, but responds to it." 
                  Style="{StaticResource ReadOnlyCheckBoxStyle}" …
Run Code Online (Sandbox Code Playgroud)

c# checkbox xaml binding controltemplate

4
推荐指数
1
解决办法
5590
查看次数

Wpf可重复使用的窗口模型/模板

我正在努力了解和学习更多关于WPF的知识.

在具有30个窗口的WPF项目中,所有窗口必须具有:

  1. 相同的工具栏(带按钮:新建,编辑,删除,查看和关闭)
  2. 相同的状态栏
  3. 每个窗口必须有一个带有2个TabItem的tabcontrol.根据窗口,第一个TabItem的内容可以不同.第二个TabItem应始终包含根据窗口具有不同列的数据网格.

"逻辑顺序"是:

  1. 打开窗户
  2. 用户在第一个TabItem中输入参数
  3. 用户单击搜索按钮
  4. 数据显示在第二个TabItem的datagarid中
  5. 用户点击新建/编辑/删除/查看
  6. 显示一个用于编辑数据的窗口

只有在选择了gridview中的行时,才能启用按钮new/edit/view和delete."逻辑顺序"对所有窗口都有效!

在winform中,您可以创建一个表单,其中包含用作模板的结构,并创建30个从模型继承的窗口.通过模板中的自定义事件,您可以了解用户是否在gridview中选择了一行,了解用户是否单击工具栏上的按钮等

例如,当用户点击New按钮生成"NewItem"事件时,该事件是自定义事件,然后在返回"NewItem"事件时从模型继承的形式打开输入表单

这是选择了第一个选项卡的表单

这是选择了第二个选项卡的fom

你可以在WPF中做类似的事情吗?

您可以在WPF中创建表单模板并创建从模板继承的窗口吗?

我希望我很清楚并抱歉我的英语不好

谢谢

wpf controltemplate

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

ControlTemplate.Triggers中更灵活的setter

我正在尝试在XAML中设置一个按钮.下面你可以看到我到目前为止创建的内容.

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#f2f2f7"/>
    <Setter Property="Padding" Value="6,4"/>
    <Setter Property="Foreground" Value="#222222" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="1" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="#b1b1c0">
                    <Border CornerRadius="1" Background="{TemplateBinding Background}" BorderThickness="1,1,1,0" BorderBrush="#f8f8fa" Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
                        </ContentPresenter>
                    </Border>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#8e8eba" />
                        <Setter Property="Foreground" Value="#f2f291" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

问题是,这个按钮有两个<Border>嵌套在一起的元素.我想BorderBrush=""IsMouseOver激活触发器时有不同的属性.例如,当我将鼠标放在按钮上时,内边框将为红色,外边框将为蓝色.

你能帮帮我吗?

c# wpf xaml controltemplate

4
推荐指数
1
解决办法
3181
查看次数