【问题标题】:Binding properties Content, Background etc in MapItemsControl PushpinMapItemsControl Pushpin 中的绑定属性内容、背景等
【发布时间】:2013-07-19 13:05:58
【问题描述】:

我有一个模型:

public class GYPushpin : GYEntity
    {   
        private GeoCoordinate _coordinate;

        public GeoCoordinate Coordinate
        {
           get
           {
             return _coordinate;
           }
           set
           {
             if (value != _coordinate)
             {
                 _coordinate = value;
                 NotifyPropertyChanged("Coordinate");
             }
           }
        }

        //.............

    }

MapItemsControl:

<toolkit:MapItemsControl>
                        <toolkit:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <toolkit:Pushpin 
                                                 GeoCoordinate="{Binding Coordinate}"                                                 
                                                 Background="{Binding Background}"  
                                                 Content="{Binding ContentPushpin}"
                                                 Tag="{Binding Tag}"   
                                                 Tap="userPushpin_Tap">                                 
                                 </toolkit:Pushpin>
                            </DataTemplate>
                        </toolkit:MapItemsControl.ItemTemplate>                        
                    </toolkit:MapItemsControl>

我在 UI 线程中使用 DataBinding 和填充 List:

Deployment.Current.Dispatcher.BeginInvoke(() =>
       {                                
           foreach (GYUser friend in friends)
              {
                   ImageBrush image = new ImageBrush()
                                    {
                                        ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://my_url" + string.Format(friend.Avatar)))
                                    };
                   Brush markerColor = friend.Sex == 1 ? new SolidColorBrush(Color.FromArgb(alpha, 71, 188, 225)) : new SolidColorBrush(Color.FromArgb(alpha, 246, 109, 128));
                   var content = new System.Windows.Shapes.Rectangle()
                                    {
                                        Fill = image,
                                        StrokeThickness = 10,
                                        Height = 50,
                                        Width = 50
                                    };

                                    var pin = new GYPushpin()
                                    {
                                        Coordinate = new GeoCoordinate()
                                        {
                                            Longitude = friend.Longitude,
                                            Latitude = friend.Latitude,
                                        },
                                        ContentPushpin = content,
                                        Background = markerColor,

                                    };
                    //add pin in binding collection
              }
       }

我有很多用户,我必须在 UI 线程中工作,因为我使用 ImageBrushShapes 等。我可以在后台工作吗?我的意思是用另一种方式绑定 Content 和 Background 属性。毕竟,MVVM 应该允许在后台使用 UI 工作。

【问题讨论】:

    标签: windows-phone-7 xaml data-binding windows-phone-8


    【解决方案1】:

    您不应在代码中创建元素,而应在 xaml 模板中创建它们。您绑定到模型中的属性(在这种情况下,它将是 Friend 类。

    <DataTemplate>
        <toolkit:Pushpin 
            GeoCoordinate="{Binding Coordinate}"                                                 
            Background="Blue"             
            Tag="{Binding Tag}"   
            Tap="userPushpin_Tap">
            <i:Interaction.Triggers>
                <ec:DataTrigger Binding="{Binding Sex}" Value="1">
                    <ec:ChangePropertyAction PropertyName="Background">
                        <ec:ChangePropertyAction.Value>
                            <SolidColorBrush Color="Red"/>
                        </ec:ChangePropertyAction.Value>
                    </ec:ChangePropertyAction>
                </ec:DataTrigger>
            </i:Interaction.Triggers>
            <Image Height="50" Width="50" Source="{Binding Avatar}"/>
        </toolkit:Pushpin>
    </DataTemplate>
    

    在这个例子中,您不需要特殊的 GYPushpin 而只需要 Friend 类。在此示例中,您的 Friend 类需要有一个完整的图像 url,并且需要一个 GeoCoordinate。

    此示例使用表达式 sdk。您需要将以下命名空间添加到您的 xaml

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
    

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      • 2013-02-18
      • 1970-01-01
      • 2020-01-04
      相关资源
      最近更新 更多