我不确定何时应该使用ContentPresenter而不是ContentControl(反之亦然).目前,我ContentControl几乎一直在使用我DataTemplate的.何时会ContentPresenter是更好的选择?为什么?
我有一个扩展器的自定义模板,它接近下面的代码.我不得不改变一些代码来取出自定义类,画笔等.
<Style TargetType="{x:Type Expander}">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter Property="VerticalContentAlignment"
Value="Top" />
<Setter Property="BorderBrush"
Value="Transparent" />
<Setter Property="FontFamily"
Value="Tahoma" />
<Setter Property="FontSize"
Value="12" />
<Setter Property="Foreground"
Value="Black" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="Margin"
Value="2,0,0,0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
<Border x:Name="Border"
SnapsToDevicePixels="true"
Background="White"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Margin="0,0,0,10"
Padding="0"
CornerRadius="8">
<DockPanel>
<Border x:Name="HeaderSite"
Background="Blue"
CornerRadius="8"
Height="32"
DockPanel.Dock="Top">
<DockPanel>
<ToggleButton Foreground="White"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="0"
MinHeight="0"
MinWidth="0"
Padding="6,2,6,2"
IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
DockPanel.Dock="Left">
</ToggleButton>
<ContentPresenter SnapsToDevicePixels="True" …Run Code Online (Sandbox Code Playgroud) 我正在开发LOB应用程序,我将需要多个对话框窗口(并且在一个窗口中显示所有内容不是一个选项/没有意义).
我希望我的窗口有一个用户控件来定义一些样式等,并且可以有几个插入内容的插槽 - 例如,模态对话框窗口的模板将有一个内容插槽和按钮(这样用户就可以提供带有绑定ICommands的内容和按钮组.
我想有这样的东西(但这不起作用):
UserControl xaml:
<UserControl x:Class="TkMVVMContainersSample.Services.Common.GUI.DialogControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
>
<DockPanel>
<DockPanel
LastChildFill="False"
HorizontalAlignment="Stretch"
DockPanel.Dock="Bottom">
<ContentPresenter ContentSource="{Binding Buttons}"/>
</DockPanel>
<Border
Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
Padding="8"
>
<ContentPresenter ContentSource="{Binding Controls}"/>
</Border>
</DockPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?我怎么告诉VS我的控件暴露了两个内容占位符,以便我可以像这样使用它?
<Window ... DataContext="MyViewModel">
<gui:DialogControl>
<gui:DialogControl.Controls>
<!-- My dialog content - grid with textboxes etc...
inherits the Window's DC - DialogControl just passes it through -->
</gui:DialogControl.Controls>
<gui:DialogControl.Buttons>
<!-- My dialog's buttons with wiring, like
<Button Command="{Binding HelpCommand}">Help</Button>
<Button Command="{Binding CancelCommand}">Cancel</Button>
<Button …Run Code Online (Sandbox Code Playgroud) 我为列表框创建了以下样式,该列表框将在某些文本旁边显示图像:
<Style x:Key="ImageListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Border">
<Grid Height="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image
x:Name="DisplayImage"
Source="{Binding Path=ThumbnailImage}"
Height="30" …Run Code Online (Sandbox Code Playgroud) 基本上,我不明白这里的真正区别是什么:
TabItem的Microsoft代码使用:
<ContentPresenter ContentSource="Header" ... />
Run Code Online (Sandbox Code Playgroud)
那么,何时使用该Content属性而不是(或除此之外)ContentSource?
我想创建一个UserControl(在这种情况下是一个带有已定义Backgroundcolors的方形按钮),它可以托管它自己的内容.
用户控件:
<UserControl x:Class="SGDB.UI.Controls.ModernButton"
xmlns:local="clr-namespace:SGDB.UI.Controls"
xmlns:converter="clr-namespace:SGDB.UI.Converter"
x:Name="_modernButton">
<Button>
<Button.Resources>
<converter:EnumToColorConverter x:Key="ColorConverter"/>
</Button.Resources>
<Button.Template>
<ControlTemplate>
<Border Width="{Binding Size, ElementName=_modernButton}" Height="{Binding Size, ElementName=_modernButton}" BorderBrush="Black" BorderThickness="0.8,0.8,3,3">
<Grid Background="{Binding BackgroundColor, ElementName=_modernButton, Converter={StaticResource ColorConverter}}">
<ContentPresenter/>
</Grid>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
现在,正如您所期望的那样,如果我在MainView中使用此控件,那么在我定义一些内容之前就可以正常工作.
使用:
<control:ModernButton Size="200" BackgroundColor="Light">
TEST
</control:ModernButton>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,"TEST"将覆盖UserControl的整个内容(整个按钮模板).我想这是因为UserControl中的Button被定义为"Content"本身,并且在定义新内容时会被覆盖.
所以最后一个问题是:有可能实现我正在寻找的东西吗?如果是的话:怎么样?我怎样才能将我在MainView中定义的内容"重定向"到我的Button模板中的自定义ContentPresenter而不是UserControls的ContentPresenter?
如果可能的话,我不想创建一个托管我的内容的新dp-propery,例如:
<controls:MordernButton Size="200" BackgroundColor="Light">
<controls:ModernButton.Content>
I don't want this, if possible
</controls:ModernButton.Content>
</controls:ModernButton>
Run Code Online (Sandbox Code Playgroud) 如果我将一段文本分配给a的Content属性,则由at渲染时生成ContentPresenter一个TextBlock控件ContentPresenter以包含该文本.
如果我创建一个适用于TextBlock属性并将其分配给它的样式,则该样式ContentPresenter似乎不适用于隐式生成的TextBlocks.
<Style x:Key="SampleStyle">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>
<ContentPresenter Content="This is a Test piece of text." Style="{StaticResource SampleStyle}"/>
Run Code Online (Sandbox Code Playgroud)
有没有办法成功地将这种风格应用于自动生成TextBlocks将其应用于所有TextBlocks(例如声明样式为TargetType="TextBlock"no Key)?
我有一个DataTemplate:
<DataTemplate x:Key="myTemplate">
...
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我想用它作为ContentTemplate一个ContentPresenter:
<ContentPresenter Content="{Binding X}">
<ContentPresenter.ContentTemplate >
<!-- ????? what goes here ????-->
</ContentPresenter.ContentTemplate>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
我如何可以使用预定义DataTemplate的我ContentPresenter?
我对此进行了很多搜索,但找不到任何软件包或有关如何使用react.js在网页上查看“.ppt”文件的指南。
我允许用户上传“.ppt”文件,并且我希望他能够在网页中查看该“Powerpoint”文件,这可能吗?
我尝试了以下方法,但没有成功...
<iframe
src={`https://view.officeapps.live.com/op/embed.aspx?src=[${linkToPPTFile}]`}
width="100%"
height="600px"
frameBorder="0"
></iframe>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
contentpresenter ×10
wpf ×9
xaml ×4
c# ×2
.net ×1
datatemplate ×1
font-family ×1
foreground ×1
powerpoint ×1
presentation ×1
reactjs ×1
styles ×1