我需要更改WPF ComboBox模板的一小部分.
如果我为Luna主题拍摄现有模板的副本并进行更改,则一切正常.但是如果用户有不同的主题,我ComboBox保留它的自定义主题(显然基于Luna),所以看起来不合适.
是否有任何方法可以覆盖模板的某些部分,因此大多数模板仍然尊重Windows主题?
我注意到模板的某些部分定义了一个mwt命名空间,并明确引用了Luna:
xmlns:mwt="...blah blah...=PresentationFramework.Luna"
Run Code Online (Sandbox Code Playgroud)
也许有一些方法让这个mwt命名空间引用当前的主题,而不是具体的Luna名称?
或者我是否必须为每个主题提供模板的自定义副本?如果MS创建一个新主题会发生什么,那么我是否必须更新我的模板以支持它?
(我正在更改的位TextBlock与组合框关闭时显示的内容有关.我不会更改下拉列表或按钮.理想情况下,我只是覆盖SelectionBoxItemTemplate它ComboBox,但这只是readonly,所以据我所知我必须覆盖整个控件模板才能进行任何更改.)
[相关问题,但没有答案:调整控制模板仍然尊重操作系统的主题?]
我有一个列表框,里面有很多渲染成本很高的项目。然而,VirtualizingStackPanel 通过只渲染可见项来处理这个问题。我想覆盖 ScrollViewer 的控件模板,因为默认的控件模板在水平和垂直滚动条之间有灰色矩形。我只是复制了微软提供的一个(ScrollViewer ControlTemplate Example),它没有灰色矩形问题。
然而,这个控制模板通过给 VirtualizingStackPanel 无限的高度来禁用虚拟化。这意味着 VirtualizingStackPanel 将呈现所有项目,因为它认为所有项目都是可见的。
在下面的演示代码中,我在列表框中显示了 10000 个项目。我通过比较运行它与 ScrollViewer 样式和没有它来验证问题。有了它,演示运行得非常慢,调整大小需要几秒钟。没有样式它非常快。我输出了一些关于 VirtualizingStackPanel 的信息来证明我的观点:
没有 ScrollViewer 样式(注释掉 XAML 中的样式):
ViewportHeight: 8
ExtentHeight: 10000
ActualHeight: 245
IsVirtualizing: True
VirtualizationMode: Standard
使用 ScrollViewer 样式:
ViewportHeight: 0
ExtentHeight: 0
ActualHeight: 272766.666666707
IsVirtualizing: True
VirtualizationMode: Standard
知道如何为不会与虚拟化混淆的 ScrollViewer 编写控件模板吗?
XAML:
<Window x:Class="VirtualTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition …Run Code Online (Sandbox Code Playgroud) 继回答类似的问题在这里,我能够设置minWidth的XAML页面上.
我想要做的是在所有ListView中的所有GridViewColumn的控件模板中完成此操作.
这可能吗?
更新:
我在下面尝试了一些简单的示例代码,但它不起作用:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}" >
<Setter Property="MinWidth" Value="200" />
</Style>
</Window.Resources>
<Grid Width="500">
<Border BorderBrush="Black" BorderThickness="2" Margin="20">
<ListView SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Header 1" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="Hello There"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Header 2" Width="Auto" />
</GridView>
</ListView.View>
</ListView>
</Border>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 我想对默认wpf组合框的PART_EditableTextBox进行更改(例如更改背景).
我尝试添加这样的样式:
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<TextBox x:Name="PART_EditableTextBox" Background="AntiqueWhite"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
但这会导致默认模板的其余部分被忽略.
有没有办法可以覆盖PART_EditableTextBox的特定属性,还是我必须复制整个控件模板并在那里进行更改?
我找不到默认的WPF ControlTemplate的CheckBox。有谁知道如何找到它?我只能找到MSDN上的SilverLight默认复选框模板。
MSDN为WPF复选框示例提供了一个自定义控件模板,该模板使用X代替复选标记。我专门在寻找WPF随附的默认检查标记样式 -我只是无法为其找到XAML。
我也尝试过使用模板XamlWriter,但无济于事。从WPF控制模板示例中下载“简单样式”模板也仅使用X代替经典的复选标记。
我的XAML是:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Image Source="X.png" HorizontalAlignment="Left"
Width="20" Height="20"
MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
</ControlTemplate>
</Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)
现在我关注MVVM.我需要更改代码以使其与ViewModel一起使用.如何MouseLeftButtonDown使用ViewModel 处理事件?
我有一个ListBox我需要在它被禁用时变灰的东西.根据用户请求,禁用它是不够的,但它也必须以不同的方式显示.耸耸肩 我看了几个其他的地方,并按照例子,似乎它应该工作,但事实并非如此.下面是一些例子我看了看:例1,例2.
这是我的代码:
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="BorderBrush" Value="Black"></Setter>
<Setter Property="Foreground" Value="LightGray"></Setter>
<Setter Property="Background" Value="LightGray"></Setter>
<Setter Property="BorderBrush" Value="LightGray"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
这似乎相当简单.我做了相同的基本过程ComboBox和TextBox成功.任何人都可以帮我看看我的代码错在哪里?如何正确地做到这一点的一个例子将是伟大的.上面列出的第一个例子似乎正是我所需要的,但正确答案是"这样做的唯一方法是覆盖模板",这对我没有帮助.
我已经尝试了几个简单的解决方案.一些其他风格可能会影响这一点,因为我们正在使用几个不同的资源字典.有人知道什么是追踪这个的好方法吗?
编辑: 我对整个解决方案进行了搜索,而ListBox正在被使用的唯一地方是我的部分,它唯一被设置样式的地方就是我设置的样式.根据MSDN,没有ListBox的"部分",所以我不可能在为其他控件设置样式的过程中无意中设置了ListBox的一部分.没有样式,当我禁用ListBox时,它被冻结但没有文本可见,并且具有默认背景.当我尝试应用Property ="Background"Value ="LightGray"时,它似乎被隐藏(即没有任何东西可见).如果有人知道为什么会这样做或如何完成我的任务,我会很感激帮助.
我最近开始使用Extended WPF Toolkit中的DataGridControl
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<xcdg:DataGridControl ItemsSource="{Binding Orders}" SelectionMode="Single" >
<xcdg:DataGridControl.View>
<xcdg:TableflowView FixedColumnCount="1" UseDefaultHeadersFooters="True" ShowRowSelectorPane="False" VerticalGridLineBrush="Green" VerticalGridLineThickness="2" HorizontalGridLineBrush="Purple" HorizontalGridLineThickness="2">
<xcdg:TableflowView.Theme>
<xcdg:ZuneNormalColorTheme/>
</xcdg:TableflowView.Theme>
</xcdg:TableflowView>
</xcdg:DataGridControl.View>
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="OrderID" IsMainColumn="True"/>
<xcdg:Column FieldName="ExternalID" />
<xcdg:Column FieldName="CustomerName" />
<xcdg:Column FieldName="Date" />
<xcdg:Column FieldName="Address" />
<xcdg:Column FieldName="Items" Width="*" />
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
没关系,一切正常.然后我添加了风格.
<Style TargetType="{x:Type xcdg:DataGridControl}">
<Setter Property="Background" Value="MediumOrchid"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
Style应用,一切都恢复正常.所以我接下来要做的就是CotrolTemplate使用Expression Blend 创建并将该模板添加到我的Style中.
<Style TargetType="{x:Type xcdg:DataGridControl}">
<Setter Property="Background"
Value="MediumOrchid" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type xcdg:DataGridControl}">
<Grid>
<Border BorderBrush="{TemplateBinding …Run Code Online (Sandbox Code Playgroud) 我有一个Telerik Tile的ControlTemplate,我如下所示:
<ControlTemplate TargetType="{x:Type ctrl:Tile}">
<Border>
<local:UserControl>
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</local:UserControl>
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我的用户控件如下:
<DockPanel>
<!-- some content -->
<ContentPresenter/>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
ControlTemplate不显示UserControl的内容.
如果我将控制模板更改为:
<ControlTemplate TargetType="{x:Type ctrl:Tile}">
<Border>
<StackPanel>
<local:UserControl/>
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</StackPanel>
</Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
它会找到内容并妥善放置.看起来ControlTemplate一旦嵌入我的UserControl中就找不到内容.有什么我可能做错了吗?
请注意,这些ControlTemplate项目出现在ItemsPresenter中.
我在app.xaml中为组合框定义了一个全局样式,如下所示:
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid Width="{TemplateBinding Width}">
<ToggleButton
Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press">
</ToggleButton>
<ContentPresenter
Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="3,3,23,3"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup
Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid
Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding …Run Code Online (Sandbox Code Playgroud) controltemplate ×10
wpf ×10
xaml ×5
styles ×2
.net ×1
background ×1
listbox ×1
listview ×1
mouseevent ×1
mvvm ×1
scrollviewer ×1
telerik ×1
themes ×1
triggers ×1
wpf-controls ×1