arcgis for silverlight 中修改DataTemplate解决思路

来源:互联网 发布:银联数据待遇 编辑:程序博客网 时间:2024/04/30 11:36

最近又开始捣鼓silverlight,其中一个问题是修改DataTemplate,控件中的值,用一下方法虽然可以获取值,但是无法修改值:

  System.Windows.DataTemplate dt2 = LayoutRoot.Resources["MyFeatureLayerInfoWindowTemplate"] as System.Windows.DataTemplate;

  StackPanel panel2 = (StackPanel)dt2.LoadContent();
   TextBlock tbCategory2 = panel2.FindName("text1") as TextBlock;

因此改变了思路,就是动态添加DataTemplate,首先创建DataTemplate,然后动态绑定到控件,这样就是先修改值,在绑定的思路,例子如下:

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot">

        <Grid.Resources>
            <esri:SimpleRenderer x:Key="MySimpleRenderer">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleFillSymbol Fill="#01FFFFFF" BorderBrush="#88000000" BorderThickness="2" />
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>

            <DataTemplate x:Key="LocationInfoWindowTemplate">
                <StackPanel Margin="2">
                    <TextBlock Text="Location:" />
                    <TextBlock Text="{Binding X, StringFormat=X\=\{0:0.000\}}" />
                    <TextBlock Text="{Binding Y, StringFormat=Y\=\{0:0.000\}}" />
                    <TextBlock  x:Name="text2" Text="111"></TextBlock>
                   
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="MyFeatureLayerInfoWindowTemplate" x:Name="DataTemplate1">
                <StackPanel Margin="2">
                    <TextBlock  Text="{Binding [STATE_NAME]}" Foreground="Black" FontSize="12" />
                    <TextBlock  x:Name="text1" Text="111"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>

        <esri:Map x:Name="MyMap" WrapAround="True" Extent="-15000000,2000000,-7000000,8000000" MouseClick="MyMap_MouseClick">
            <esri:ArcGISTiledMapServiceLayer ID="Street Map"
                    Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
            <esri:FeatureLayer ID="MyFeatureLayer"
                               Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"
                               OutFields="STATE_NAME,POP2007"
                               Renderer="{StaticResource MySimpleRenderer}"
                                />
        </esri:Map>

        <esri:InfoWindow x:Name="MyInfoWindow"
                         Padding="2"
                         CornerRadius="20"
                         Background="LightSalmon"
                         Map="{Binding ElementName=MyMap}"
                       
                         MouseLeftButtonUp="MyInfoWindow_MouseLeftButtonUp" />
    </Grid>

</UserControl>

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Toolkit;
using System.IO;
using System.Windows.Markup;
using System.Text;
namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        TextBlock tb1 = new TextBlock();
        public MainPage()
        {
            InitializeComponent();
           
        }

//动态创建模板
        private DataTemplate ss()
        {

            string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                                                               xmlns:x=""
http://schemas.microsoft.com/winfx/2006/xaml""  
                                                               xmlns:local=""clr-namespace:GalaxyErp.Module""> 
                                         
                                           <StackPanel VerticalAlignment=""Stretch"" HorizontalAlignment=""Stretch""> 
                                               <TextBlock  Text=""{Binding [STATE_NAME]}"" Foreground=""Black"" FontSize=""12"" />
                                              <TextBlock  x:Name=""text1"" Text=""4343""></TextBlock>
                                           </StackPanel> 
                                       </DataTemplate>";
           // Stream s = new MemoryStream(Encoding.UTF8.GetBytes(xaml));
            return (DataTemplate)XamlReader.Load(xaml); 
        }
    
        private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
  

            FeatureLayer featureLayer = MyMap.Layers["MyFeatureLayer"] as FeatureLayer;
            System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);

            // Account for difference between Map and application origin
            GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);
            System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);

            IEnumerable<Graphic> selected =
                featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);

            foreach (Graphic g in selected)
            {

                MyInfoWindow.Anchor = e.MapPoint;
                MyInfoWindow.IsOpen = true;
                MyInfoWindow.ContentTemplate = ss();//

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Toolkit;
using System.IO;
using System.Windows.Markup;
using System.Text;
namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        TextBlock tb1 = new TextBlock();
        public MainPage()
        {
            InitializeComponent();
           
        }
        private DataTemplate ss()
        {

            string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                                                               xmlns:x=""
http://schemas.microsoft.com/winfx/2006/xaml""  
                                                               xmlns:local=""clr-namespace:GalaxyErp.Module""> 
                                         
                                           <StackPanel VerticalAlignment=""Stretch"" HorizontalAlignment=""Stretch""> 
                                               <TextBlock  Text=""{Binding [STATE_NAME]}"" Foreground=""Black"" FontSize=""12"" />
                                              <TextBlock  x:Name=""text1"" Text=""4343""></TextBlock>
                                           </StackPanel> 
                                       </DataTemplate>";
           // Stream s = new MemoryStream(Encoding.UTF8.GetBytes(xaml));
            return (DataTemplate)XamlReader.Load(xaml); 
        }
    
        private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
  

            FeatureLayer featureLayer = MyMap.Layers["MyFeatureLayer"] as FeatureLayer;
            System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);

            // Account for difference between Map and application origin
            GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);
            System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);

            IEnumerable<Graphic> selected =
                featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);

            foreach (Graphic g in selected)
            {

                MyInfoWindow.Anchor = e.MapPoint;
                MyInfoWindow.IsOpen = true;
                MyInfoWindow.ContentTemplate = ss();//绑定模板
                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                MyInfoWindow.Content = g.Attributes;
                return;
            }

            InfoWindow window = new InfoWindow()
            {
                Anchor = e.MapPoint,
                Map = MyMap,
                IsOpen = true,
                Placement=InfoWindow.PlacementMode.Auto,
                ContentTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as System.Windows.DataTemplate,
                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                Content = e.MapPoint
            };
            LayoutRoot.Children.Add(window);
        }
       
        private void MyInfoWindow_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MyInfoWindow.IsOpen = false;
        }

    }
}


                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                MyInfoWindow.Content = g.Attributes;
                return;
            }

            InfoWindow window = new InfoWindow()
            {
                Anchor = e.MapPoint,
                Map = MyMap,
                IsOpen = true,
                Placement=InfoWindow.PlacementMode.Auto,
                ContentTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as System.Windows.DataTemplate,
                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                Content = e.MapPoint
            };
            LayoutRoot.Children.Add(window);
        }
       
        private void MyInfoWindow_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MyInfoWindow.IsOpen = false;
        }

    }
}

 

0 0
原创粉丝点击