这段时间写ControlTemplate的时候发现绑定的时候有些问题需要总结:

实例ControlTemplate如下:

<UserControl x:Class="ArcGISWpfMarkTest.TestSymbol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
xmlns:local="clr-namespace:ArcGISWpfMarkTest"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<esri:MarkerSymbol x:Key="Small" OffsetX="60" OffsetY="72">
<esri:MarkerSymbol.ControlTemplate>
<ControlTemplate>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageSelected">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="label0">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageSelected">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="label0">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.2*"></RowDefinition>
<RowDefinition Height="0.2*"></RowDefinition>
<RowDefinition Height="0.8*"></RowDefinition>
</Grid.RowDefinitions>
<Label x:Name="label0" Visibility="Visible" Content="{Binding LabelContent1,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Grid.Row="0" BorderThickness="0.5" BorderBrush="White" HorizontalContentAlignment="Center" Padding="0" Margin="0" Background="#2b5e93" VerticalContentAlignment="Center" Foreground="White" Width="120" Height="20" FontSize="12"/>
<Label x:Name="label1" Visibility="Visible" Content="{Binding LabelContent2,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Grid.Row="1" BorderThickness="0.5" BorderBrush="White" HorizontalContentAlignment="Center" Padding="0" Margin="0" Background="#2b5e93" VerticalContentAlignment="Center" Foreground="White" Width="120" Height="20" FontSize="12"/>
<Image Name="ImageNormal" Grid.Row="2" Source="/Images/small_police.png" Visibility="Visible"/>
<Image Name="ImageSelected" Grid.Row="2" Source="/Images/small_police_hit.png" Visibility="Hidden"/>
</Grid>
</Grid>
</ControlTemplate>
</esri:MarkerSymbol.ControlTemplate>
</esri:MarkerSymbol>
</UserControl.Resources>
<Grid> </Grid>
</UserControl>

这个ControlTemplate是我定义的Arcgis runtime for wpf中MarkerSymbol的ControlTemplate

首先,我遇到第一个问题是:在map窗口的主程序中调用TestSymbol这个类的实例来引用它的ControlTemplate资源时候遇到两个Label的Content无法绑定

情况如下:TestSymbol类中我定义了LabelContent1和LabelContent2两个属性,在Map窗口的类中:

TestSymbol testSymbol=new TestSymbol();

testSymbol.TryFindResources("Small") as Symbol作为一个Graphic点位的Symbol,这样发现ControlTemplate定义的绑定写法其实根本就错误的。

ControlTemplate作为资源是被Map的主窗口调用,这样它沿着可视树查找的时候的DataContext就是Map的主窗口,所以LabelContent1和LabelContent2两个属性应该定义到

Map的主窗口类中,并且绑定应该这样写Content="{Binding LabelContent1,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"

但是这样做的话对我的程序来说没有意义,我不可能每个点位都分别定义LabelContent1和LabelContent2两个属性,所以只能另外想办法了。

我重新定义了一个类用来存每个点位的相关信息

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging; namespace ArcGISWpfMarkTest
{
class TestElement:Graphic
{
private string manName; public string ManName
{
get { return manName; }
set { manName = value; }
} private string phoneNo; public string PhoneNo
{
get { return phoneNo; }
set { phoneNo = value; }
}
public TestElement()
{ }
public TestElement(string ManName,string PhoneNo)
{
this.ManName = ManName;
this.PhoneNo = PhoneNo;
ImageNormal = "pack://application:,,,/Source/Images/man32.png";
ImageSelected = "/Source/Images/man_selected32.png";
Symbol = GetMarkerSymbol() as Symbol;
} public MarkerSymbol GetMarkerSymbol()
{
MarkerSymbol markerSymbol = new MarkerSymbol();
markerSymbol.OffsetX = 60;
markerSymbol.OffsetY = 72;
markerSymbol.ControlTemplate = GetControlTemplate();
return markerSymbol;
} public string ImageNormal { get; set; }
public string ImageSelected { get; set; } private ControlTemplate GetControlTemplate()
{
string template = "<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name=\"SelectionStates\"><VisualState x:Name=\"Unselected\" /><VisualState x:Name=\"Selected\"><Storyboard><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty=\"(UIElement.Visibility)\" Storyboard.TargetName=\"ImageNormal\"><DiscreteObjectKeyFrame KeyTime=\"0\" Value=\"{x:Static Visibility.Hidden}\"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty=\"(UIElement.Visibility)\" Storyboard.TargetName=\"ImageSelected\"><DiscreteObjectKeyFrame KeyTime=\"0\" Value=\"{x:Static Visibility.Visible}\"/></ObjectAnimationUsingKeyFrames></Storyboard></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><Grid><Grid.RowDefinitions><RowDefinition Height=\"0.2 * \"></RowDefinition><RowDefinition Height=\"0.2 * \"></RowDefinition><RowDefinition Height=\"0.8 * \"></RowDefinition></Grid.RowDefinitions><Label Visibility=\"Visible\" Grid.Row=\"0\" BorderThickness=\"0.5\" BorderBrush=\"White\" HorizontalContentAlignment=\"Center\" Padding=\"0\" Margin=\"0\" Background=\"" + "#2b5e93" + "\" VerticalContentAlignment=\"Center\" Foreground=\"White\" Content=\"" + ManName + "\" Width=\"" + "120" + "\" Height=\"" + "20" + "\" FontSize=\"" + "12" + "\"/><Label Visibility=\"Visible\" Grid.Row=\"1\" BorderThickness=\"0.5\" BorderBrush=\"White\" HorizontalContentAlignment=\"Center\" Padding=\"0\" Margin=\"0\" Background=\"" + "#2b5e93" + "\" VerticalContentAlignment=\"Center\" Foreground=\"White\" Content=\"" + PhoneNo + "\" Width=\"" + "120" + "\" Height=\"" + "20" + "\" FontSize=\"" + "12" + "\"/><Image Name=\"ImageNormal\" Grid.Row=\"2\" Source=\"" + ImageNormal + "\" Visibility=\"Visible\"/><Image Name=\"ImageSelected\" Grid.Row=\"2\" Source=\"" + ImageSelected + "\" Visibility=\"Hidden\"/></Grid></Grid></ControlTemplate>";
var tem = XamlReader.Parse(template);
return tem as ControlTemplate;
}
}
}

  这个TestElement类的关键思想是把xmal代码定义成字符串格式,然后这个字符串是依赖于TestElement类的自身属性而动态生成的,不同实例传入的属性不同,所以突破了xmal绑定机制的限制,可以成功的为每个点位传入不同的属性值了,这个xmal代码字符串通过以下代码成功转为ControlTemplate了。

 var tem = XamlReader.Parse(template);
return tem as ControlTemplate; 以上做法有点违背wpf思想设计初衷,我也是没办法才如此做的,谁叫我的WPF技能不够高呢。。 在查找使用C#代码后台定义ControlTemplate的时候,发现这篇博文的方法也可行,只是再写布局的时候有些麻烦,我暂时还没有去实现
http://blog.csdn.net/zyloveyrf/article/details/6736844 还有一个问题总结:
在写Image的资源路径的时候遇到这样一个问题:
在Demo程序中这样定义Image的Resource可行

"pack://application:,,,/Source/Images/man32.png" 绝对路径
 "/Source/Images/man_selected32.png" 相对路径

但是有时候上面的写法无效,非要下面的写法才行:

"pack://application:,,,/SDGPS_ManLayer;component/Source/Images/man32.png";
"pack://application:,,,/SDGPS_ManLayer;component/Source/Images/man_selected32.png";

这种情况发生在我的图层是单独编译成一个DLL,与MAP的主程序分离设计的。

我想可能是程序资源集引用的相关问题,以后再做研究。

WPF的Resource路径参考:

http://www.cnblogs.com/kushisei/p/5747708.html

https://msdn.microsoft.com/library/aa970069(v=vs.100).aspx

挂两个外链,管理员请不要删我文,如违规可联系我自行删除,QQ:919497132

苏州空调维修 苏州冰箱维修 上海注册公司

WPF后台写ControlTemplate总结的更多相关文章

  1. WPF后台设置xaml控件的样式System.Windows.Style

    WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵   http://3w.suchso.com/projecteac-tual/wpf-zhi ...

  2. WPF 后台C#设置控件背景图片

    原文:WPF 后台C#设置控件背景图片 以前的程序中有做过,当时只是记得uri很长一大段就没怎么记.今天有人问了也就写下来.   这是一个Button,设置了Background后的效果. 前台的设置 ...

  3. WPF 后台获得 数据模板里的内容控件(DataTemplate)

    原文:WPF 后台获得 数据模板里的内容控件(DataTemplate) 假如      <Window.Resources> 里 有一个 Datatemplate 我想获得TextBlo ...

  4. C# WPF后台动态添加控件(经典)

    概述 在Winform中从后台添加控件相对比较容易,但是在WPF中,我们知道界面是通过XAML编写的,如何把后台写好的控件动态添加到前台呢?本节举例介绍这个问题. 这里要用到UniformGrid布局 ...

  5. WPF中的ControlTemplate(控件模板)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板)     ...

  6. WPF后台访问XAML元素

    当我们需要从后台访问xaml文件时,我们可以通过这样的方式来操作: private void button1_Click(object sender, RoutedEventArgs e) { Sys ...

  7. excel导出的时候从程序后台写到excel里的是文本,所以无法在excel中计算怎么办?

    文章引用自:http://www.cnblogs.com/rayray/p/3414452.html excel导出的时候从程序后台写到excel里的是文本,所以无法在excel中计算怎么办? 需要导 ...

  8. wpf后台设置颜色(背景色,前景色)

    有时候你是不是也会遇到要在wpf后台给某个控件设置背景色或者给文字设置前景色的情况? 本人最近看到一个从自定义的combobox读取系统字体和颜色的实例,该实例实现了随字体combobox选项改变而改 ...

  9. WPF中的ControlTemplate(控件模板)

    原文:WPF中的ControlTemplate(控件模板) WPF中的ControlTemplate(控件模板)                                             ...

随机推荐

  1. 例题:填词-poj

    问题描述 Alex喜欢填词游戏.填词游戏是一个非常简单的游戏.填词游戏包括一个N X M大小的矩形方格盘和P个单词.玩家需要把每个方格中填上一个字母使得每个单词都能在方格盘上被找到.每个单词都能被找到 ...

  2. 使用Maven Archetype插件构建Maven工程原型模板

    创建原型模板 1.在空目录运行archetype:generate上面的命令,待下载完必要的jar包后,首先需要输入内置的原型编号: 1 Choose archetype: 2 1: internal ...

  3. SQL Count(*)函数,GROUP_By,Having的联合使用

    COUNT(*)  函数返回在给定的选择中被选的行数. 语法:SELECT  COUNT(*) FROM  table 使用:现在有一个表,名叫app_category,从Navicat中可以看到表中 ...

  4. mysql 系统性浅聊 myisam 存储引擎【原创】

    >>思维导图 >>介绍 mysql中的存储引擎都是以插件的形式存在,目前用的最多存储引擎就是innodb和myisam.MySQL5.5.5以后(包括5.5.5)默认使用Inn ...

  5. 51Nod 1267 4个数和为0 二分

    给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No".Input第1行,1个数N,N为数组的长度(4 <= ...

  6. javascript设计模式——适配器模式

    前面的话 适配器模式的作用是解决两个软件实体间的接口不兼容的问题.使用适配器模式之后,原本由于接口不兼容而不能工作的两个软件实体可以一起工作.适配器的别名是包装器(wrapper),这是一个相对简单的 ...

  7. java学习笔记IO之字节输入输出流

    IO字节输入输出流 OutputStream:字节输出流 该抽象类是所有字节输出流的超类: 定义了一些共性的成员方法: 1.写入一个字节 void write(int b);//b表示字节 2.写入字 ...

  8. 两个port贴合七夕主题,百度输入法的“情感营销”策略

        一年一度的七夕佳节是情侣.夫妻之间传情达意.诉说衷肠的最佳时节.基于这一背景.一些传统企业.互联网公司也会针对性的推出一些营销策划,使产品和服务更贴近用户需求,更"接地气" ...

  9. Timus 1180. Stone Game 游戏题目

    Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns ...

  10. ITM事件直接接收并解析

    之前在实施一个监控项目时.客户由于买了IBM的小机.当前就赠送了TIVOLI的系统监控软件一套,客户也在他们的生产环境中部署了ITM的监控.由于没有购买IBM的netcool,无法集中管理告警事件,请 ...