WPF:如何使用Custorm控件
你好,
我创建了一个名为Base_Dialog
的自定义控件库,并内置了Base_Dialog
--- BaseDlg
像这样:
Hello,
I have created a custom control Library called Base_Dialog
and I built in Base_Dialog
---BaseDlg
like this:
<pre lang="cs">using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows;<br />
using System.Windows.Controls;<br />
using System.Windows.Data;<br />
using System.Windows.Documents;<br />
using System.Windows.Input;<br />
using System.Windows.Media;<br />
using System.Windows.Media.Imaging;<br />
using System.Windows.Navigation;<br />
using System.Windows.Shapes;<br />
namespace Base_Dialog<br />
{<br />
public class BaseDlg : Window<br />
{<br />
static BaseDlg()<br />
{<br />
DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseDlg), new FrameworkPropertyMetadata(typeof(BaseDlg)));<br />
}<br />
}<br />
}</pre>
并且在Generic.xaml
and in Generic.xaml
<br />
<pre lang="xml"><ResourceDictionary<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
xmlns:local="clr-namespace:Base_Dialog"<br />
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"><br />
<Style TargetType="{x:Type local:BaseDlg}" ><br />
<Setter Property="Template"><br />
<Setter.Value><br />
<ControlTemplate TargetType="{x:Type local:BaseDlg}"><br />
<Grid Background="Salmon"><br />
<TextBlock Text="kdjdfg"/><br />
</Grid><br />
</ControlTemplate><br />
</Setter.Value><br />
</Setter><br />
</Style><br />
</ResourceDictionary></pre><br />
<br />
我在AssemblyInfo.cs
文件中编写了以下代码:
and I wrote the following code in the AssemblyInfo.cs
file:
<br />
[assembly: XmlnsDefinition("http://schemas.sheva.com/winfx/2006/xaml/presentation", "Base_Dialog")]
然后打开wpf应用程序名称((wpf))并添加引用项目((Base_Dialog))
xaml代码是:
then open wpfapplication name((wpf))and add references Project ((Base_Dialog))
xaml code is:
<br />
<pre lang="xml"><local:BaseDlg x:Class="wpf.Box"<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
xmlns:local="http://schemas.sheva.com/winfx/2006/xaml/presentation"<br />
Title="Box" Height="300" Width="300"><br />
<Grid><br />
<Button Content="enas" Height="30" Width="30" HorizontalAlignment="Left" VerticalAlignment="Top"/><br />
</Grid><br />
</local:BaseDlg></pre>
我已经在BaseDlg
和Box.xaml
更改样式中创建了一个网格,但是当我在Box.xaml
中创建按钮时,Box.xaml
却没有更改.
Box.xaml仅来自BaseDlg的文本块
任何帮助将不胜感激.
谢谢.
I have created a grid in BaseDlg
the Box.xaml
change style but when I create a button in Box.xaml
the Box.xaml
has not changed.
The Box.xaml just content Textblock from BaseDlg
Any help is greatly appreciated.
Thanks.
在模板中,您需要添加ContentPresenter.这是所有控件的去向.
In your template you need to add a ContentPresenter. This is where all of the controls will go.
<ContentPresenter x:Name="windowContent" Content="{TemplateBinding Property=ContentControl.Content}" Margin="4"/>