WPF *.xaml中的ControlTemplate如何写成过程式代码(*.cs)

Jave.Lin 2013-11-20 03:05:07
DesignerLayerNameTextBox.cs文件摘要内容:

using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWpfTest.control
{
public class DesignerLayerNameTextBox : Control
{
public DesignerLayerNameTextBox()
{
// 这里如何写成DesignerLayerStenclis.xaml中的定义呢?
// 我这里不建议使用:XamlWrite或是XamlReader,的读写字符串方式;
// 我只想用纯过程式代码;比较容易把控;
// 因为我有好几个地方的ItemsControl的Items.Add()或是Item.Remove()的控件都有对应的模板;
// 而,很多情况,Remove之后,再Add回去,发现,好多内容,自动还原成ControlTemplate定义的内容;
// 原因不明,如果能写成过程式,估计,能比较容易发现问题;
}
}
}


DesignerLayerStenclis.xaml文件内容:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:baseControl="clr-namespace:MyWpfTest.control"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!--图层名称的文本框-->
<Style TargetType="{x:Type baseControl:DesignerLayerNameTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type baseControl:DesignerLayerNameTextBox}">
<Grid>
<TextBox x:Name="TextBox" Text="图层1"
HorizontalAlignment="Stretch" VerticalContentAlignment="Center"
BorderThickness="0" Background="Transparent" IsHitTestVisible="False"
Focusable="False"/>
<Rectangle
x:Name="HotAreaRect"
Fill="Transparent"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Focusable ="False"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</ResourceDictionary>
...全文
222 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jave.Lin 2013-12-03
  • 打赏
  • 举报
回复
该问题自行解决了。 我在stackoverflow写了问题贴中的DEMO有相关代码; http://stackoverflow.com/questions/20342906/wpf-how-can-i-get-template-element-on-custom-controls-after-new-instance-immed/20342951?noredirect=1#20342951 好吧,冷清的CSDN,实在解决不了,我把这问题关了。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧