- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经创建了一个模板化控件。我对默认样式所做的只是添加一个内容展示器。我还在 App.xaml 文件中引用了 Generic.xaml。
<Style TargetType="local2:TestingControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local2:TestingControl">
<Border
Height="200px"
Background="Green">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public sealed class TestingControl : Control
{
public TestingControl()
{
this.DefaultStyleKey = typeof(TestingControl);
}
}
我没有对控件的 .cs 代码进行任何更改。我尝试设置内容,但被告知该控件不允许直接内容。
<StackPanel>
<local1:TestingControl >
Testing
</local1:TestingControl>
</StackPanel>
如果我尝试使用用户控件,同样的方法非常有效。
最佳答案
要处理自定义模板化控件中的 XAML 内容,您必须从 ContentControl
派生控件。或者继续继承自 Control
, 实现自定义 ContentProperty
并绑定(bind) ContentPresenter
使用 ContentControl
更容易一些,下面是您可能最终得到的代码。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="using:SmallTests2018">
<Style TargetType="local:TemplatedControlWithContent" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TemplatedControlWithContent">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Viewbox>
<Grid>
<Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Ellipse Width="10" Height="10" Fill="#80808080" />
</Viewbox>
<ContentPresenter />
</Grid>
</Viewbox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Border
绑定(bind)到模板化控件属性,这样使用它的开发人员可以更好地控制外观。using System;
using Windows.UI.Xaml.Controls;
namespace SmallTests2018
{
public sealed class TemplatedControlWithContent : ContentControl
{
public TemplatedControlWithContent()
{
DefaultStyleKey = typeof(TemplatedControlWithContent);
}
}
}
ContentControl
。 <Page
x:Class="SmallTests2018.TemplatedControlWithContentPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SmallTests2018"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<local:TemplatedControlWithContent>
<TextBlock>Hello World!</TextBlock>
</local:TemplatedControlWithContent>
</Page>
关于c# - 模板控件中的 ContentPresenter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53697165/
我刚刚开始使用样式和控件模板,并创建了以下样式来将按钮显示为网格中的文本。我想将字体样式嵌入到下划线样式中,但还没有弄清楚。
我有一个 DataTemplate : ... 我想用它作为 ContentTemplate的 ContentPresenter : 如何使用预定
我正在尝试为我的 ItemsControl 使用单选模式。所以我将 ItemsControl 更改为 ListBox 但是出现异常 "A style intended for type 'Conten
基本上,我不了解这里的真正区别是: TabItem的Microsoft代码使用: 那么,什么时候可以使用Content属性代替(或补充使用)ContentSource? 最佳答案 此属性仅在以下情况
我遇到了 ContentPresenter 根据样式是否位于窗口中而表现异常的问题。资源或资源字典中。具体来说,我将默认 TextBlock 的前景设置为黑色,然后将默认按钮样式中的前景值设置为白色。
我在 ControlTemplate 中有一个 ContentPresenter,试图布局一个 TabItem。我希望标题内 TextBlock 的前景色在选择选项卡时更改颜色。我的模板如下:
我在 Themes\Generic.xaml 下有下面的代码 现在我需要在代码中获取 cPresenter 我该怎么做? 事实上,我尝试将 Silverlight 实现转换为 WPF 代码。我想使用类
我只是在玩一个简单的 ContentPresenter。
我的 View 中有一个 ContentControl,它数据绑定(bind)到我的 View 模型的 CurrentItem 属性。通过 CurrentItem 公开的对象每个都有自己的 DataT
我覆盖了 wpf 扩展器的模板。 header 有 ContentPresenter 我试图为里面的所有 TextBlocks 添加我的样式。 如果我将标题
好的,我在网格单元中有一个 contentpresenter:
我已经覆盖了 ComboBox为了在组合的末尾添加一个额外的按钮 - 我正在使用它来帮助浏览我的应用程序。 它是一个 M-V-VM 应用程序,具有编辑-保存/取消机制,可根据用户是否处于“编辑模式”来
是否可以定义 ContentPresenter 的“呈现行为”?以便将填充应用于其内容? 现在我有一个 ContentPresenter并定义所有 UserControl 的边距可以成为此 Conte
我正在尝试基于数据触发器切换 contentpresenter 的内容。我想在 contentpresenter.content 中显示用户控件,如果我设置了值,否则我需要显示错误消息。但是绑定(bi
我正在使用以下产品:VS 2010、.Net 4、WPF 和 .Net Memory Profiler 版本 4.6.40.0 Pro。它们都在 Win7 64 位上运行。我一直在使用 .Net Me
我有以下 xaml:
我有 2 个 ContentPresenter fixedContentPresenter 和 resizableContentPresenter 并且显然是 Generic.xaml 中的内容。当
我正在使用 ItemsControl 生成基于我的模型的控件列表。查看可视化树时,我注意到每个呈现的控件都包装在一个 ContentPresenter 中。添加的控件是第 3 方控件,旨在显示每个控件
我有一个 ListBox,它绑定(bind)到动态创建的 UserControls 的 ObesvableCollection。 .
例如: 然后,PopUpView 中的 View 将在中间有一个边框,其中包含要显示的消息和一个半透明的背景,使
我是一名优秀的程序员,十分优秀!