我有资源字典文件(MenuTemplate.xaml,ButtonTemplate.xaml等),我想在多个单独的应用程序中使用.我可以将它们添加到应用程序的程序集中,但如果我在一个程序集中编译这些资源并让我的应用程序引用它,那就更好了吧?
构建资源程序集后,如何在我的应用程序的App.xaml中引用它?目前,我使用ResourceDictionary.MergedDictionaries来合并各个字典文件.如果我在一个程序集中有它们,我怎么能在xaml中引用它们?
WPF中a ControlTemplate和a有什么区别DataTemplate?
如果您ResizeMode="CanResizeWithGrip"在WPF上设置,Window则右下角会显示调整大小的手柄,如下所示:

如果你设置WindowStyle="None"了标题栏,标题栏会消失,但灰色斜角边缘会保留,直到你设置为止ResizeMode="NoResize".不幸的是,通过设置这种属性组合,调整大小的抓握也会消失.
我通过习惯覆盖了Window's' .我想自己指定窗口的边框,我不需要用户能够从四面调整窗口大小,但我确实需要调整大小.ControlTemplateStyle
有人可以详细说明满足所有这些标准的简单方法吗?
Window除了我指定的那个之外,没有边界ControlTemplate.好的......这让我挠头.我有两个WPF控件 - 一个是用户控件,另一个是自定义控件.我们称他们为UserFoo和CustomFoo.在CustomFoo的控件模板中,我使用UserFoo的一个实例,这是一个命名的部分,所以我可以在应用模板后到达它.这很好.
现在,UserFoo和CustomFoo都有一个Text定义的属性(独立地,即不是使用AddOwner的共享DP.不要问...)这两个都被声明为......
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(UserFoo), // The other is CustomFoo
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
null,
null,
true,
UpdateSourceTrigger.PropertyChanged
)
);
Run Code Online (Sandbox Code Playgroud)
请特别注意,模式设置为TwoWay,UpdateSourceTrigger设置为PropertyChanged,两者都设置为.
因此,在CustomFoo的样式模板中,我想将CustomFoo的Text属性绑定为内部UserFoo的Text属性的源.通常,这很容易.你只需将UserFoo的文本属性设置为"{TemplateBinding Text}",但由于某种原因,它只采用一种方式(即从FreeFoo中正确设置UserFoo,但不是相反),即使再次,两个DP都设置为双向!但是,当使用相对源绑定而不是模板绑定时,它工作得很好!嗯......哇?
// This one works
Text="{Binding Text, RelativeSource={RelativeSource AncestorType={local:CustomFoo}}, Mode=TwoWay}"
// As does this too...
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
// But not this one!
Text="{TemplateBinding Text}"
Run Code Online (Sandbox Code Playgroud)
什么给出了什么?我错过了什么?
我最近做了一个UserControl,花了很长时间,因为我不得不使用自定义依赖属性等等...
无论如何,它只是一堆3个控件:TextBox,Popup with Hierarchical Tree.
现在我意识到我可能只能写一个ControlTemplate.那么使用UserControl有什么好处?
如何在WPAM中以XAML格式(可视化树)获取现有控件的controltemplate?这有助于在现有模板的帮助下创建新的controltemplate.
根据此MSDN链接,
没有办法只替换控件的部分可视树; 若要更改控件的可视树,必须将控件的Template属性设置为新的完整ControlTemplate
.
我试图禁用GridViewColumnHeader的点击行为(我需要删除原始控件模板中的一些触发器),但我无法找到本机"ColumnHeaderContainerStyle".我发现的所有人似乎已经做了一些定制,很难获得原始的外观和感觉.
有人可以建议我如何/在哪里可以获得原生WPF控件中定义的原始控件模板?
谢谢你的关注.
是否有任何"最佳实践"方法来替换默认模板的一部分.当前的用例是树视图.默认情况下,树视图具有这种小三角形形状以展开和折叠.
如果我替换整个控件模板,我知道如何替换它们,如下面的代码所示.我不确定是否有办法"保持所有默认值,只需更改XY".它不是一种风格,我基本上需要替换现有控件模板的一部分.
为了说明,请查看以下XAML.第一个较小的块是我希望能够适应的相关XAML.
较大的第二和第三部分基本上是默认模板的副本,仅用于从头开始管理"已更改"部分.
有没有更好的方法来做到这一点,在下半场节省长而混乱的XAML?
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid
Width="15"
Height="13"
Background="Transparent">
<Path x:Name="ExpandPath"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="1,1,1,1"
Fill="Black"
Data="M 4 0 L 8 4 L 4 8 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked"
Value="True">
<Setter Property="Data"
TargetName="ExpandPath"
Value="M 0 4 L 8 4 L 4 8 Z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19"
Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> …Run Code Online (Sandbox Code Playgroud) 我有一个ListBox,它的ItemTemplate绑定到DataTemplate.我的问题是我无法将模板中的元素拉伸到ListBox的整个宽度.
<ListBox x:Name="listPeople" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Margin="0,0,0,0" Background="{x:Null}" SelectionMode="Extended" Grid.Row="1"
ItemTemplate="{StaticResource PersonViewModel.BrowserDataTemplate}"
ItemsSource="{Binding Mode=OneWay, Path=SearchResults}" >
</ListBox>
<DataTemplate x:Key="PersonViewModel.BrowserDataTemplate">
<ListBoxItem HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,5,5,5">
<Border Opacity=".1" x:Name="itemBorder" Background="#FF000000"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
CornerRadius="5,5,5,5" MinWidth="100" Height="50"/>
</Grid>
</ListBoxItem>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如您所见,我在网格中添加了一个边框以指示模板的宽度.我的目标是看到此边框扩展到列表框的整个宽度.目前它的宽度由其内容或MinWidth决定,这是目前保持可见的唯一内容.
我正在编写一个WPF控件,它是Button的子类.然后我在Themes\generic.xaml中提供了一个默认样式,它看起来像这样(简化):
<Style TargetType="{x:Type WPFControls:MyButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type WPFControls:MyButton}">
<Button
x:Name="PART_Button"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
我希望用户有机会更改控件的背景,但如果他没有,我想提供默认值.我该怎么做?
当我在发布的代码中执行它时,Background和BorderBrush为null(=不存在),除非用户明确指定它们(这有效地强制用户总是提供一些值),但标准的Windows控件(如Button)提供了默认的外观,仍然可以由用户定制.如何在我的控制下做到这一点?
谢谢!
Michael Morton解决方案:
您可以在样式中提供默认设置:
<Style TargetType="{x:Type TestTemplate:MyButton}">
<Setter Property="Background" Value="Red" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TestTemplate:MyButton}">
<Button
x:Name="PART_Button"
IsEnabled="{TemplateBinding IsEnabled}"
Content="{TemplateBinding Content}"
Background="{TemplateBinding Background}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
用法:
<StackPanel>
<TestTemplate:MyButton Background="Blue">Explicitly blue</TestTemplate:MyButton>
<TestTemplate:MyButton>Naturally red</TestTemplate:MyButton>
</StackPanel>
Run Code Online (Sandbox Code Playgroud) controltemplate ×10
wpf ×9
.net ×3
datatemplate ×2
styles ×2
binding ×1
c# ×1
default ×1
listbox ×1
mvvm ×1
resizegrip ×1
silverlight ×1
window ×1
xaml ×1