简体   繁体   English

Silverlight 4-绑定不适用于POCO对象的枚举类型吗?

[英]Silverlight 4 - Binding does not work on enum types of POCO objects?

I have a WCF RIA POCO object -> Traffic light which has an enum type describing it's state. 我有一个WCF RIA POCO对象->交通灯,它具有描述其状态的枚举类型。

[DataContract]
public class TrafficLight
{
    [Key]
    [DataMember]
    public long Id { get; set; }    

    [DataMember]
    public Longitude Longitude { get; set; }

    [DataMember]
    public Latitude Latitude { get; set; }

    [DataMember]
    public TrafficLightState SelectedLight { get; set; }
}

[DataMember]
public enum TrafficLightState
{
    [EnumMember]
    Red = 0,

    [EnumMember]
    Yellow = 1,

    [EnumMember]
    Green = 2
}

So given a WCF RIA service I needed to get this traffic light object to the Silverlight client application: 因此,考虑到WCF RIA服务,我需要将此交通灯对象发送到Silverlight客户端应用程序:

public IEnumerable<TrafficLight> GetTrafficLightsForCity(int cityId)
{
    return Data.GetTrafficLightsForCity(cityId).AsEnumerable();
}

So that works fine. 这样就可以了。 I get my list and all is good until I decide to bind the list to a grid that will display my traffic lights... (PS. This is a game kinda like sim city): 我得到了我的列表,一切都很好,直到我决定将列表绑定到一个显示我的交通信号灯的网格...(PS。这有点像模拟城市):

The way the traffic lights are displayed in the grid is via a small control that displays the state of the light -> red, yellow, green, plus some other stuff. 交通灯在网格中的显示方式是通过一个小的控件来显示交通灯的状态->红色,黄色,绿色以及其他一些东西。 For now I will just show you the Light status and ID: 现在,我仅向您显示灯光状态和ID:

<UserControl x:Name="MiniTrafficLightControl" x:Class="UI.MiniTrafficLight"
    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"
    mc:Ignorable="d"
    d:DesignHeight="15" 
    d:DesignWidth="15"
    Width="15" Height="15">
    <StackPanel Orientation="Horizontal">
        <Border x:Name="BrdGreen" Visibility="Visible" CornerRadius="10" Width="15" Height="15" Margin="0" ToolTipService.ToolTip="Runing" d:IsLocked="True">
        <Border.Background>
            <RadialGradientBrush  GradientOrigin="0.4,0.3" Center="0.4,0.3" RadiusX="0.5" RadiusY="0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="LightGreen" Offset="0" />
            <GradientStop Color="Green" Offset="0.95" />
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
        </Border.Background>
    </Border>
    <Border x:Name="BrdYellow" Visibility="Collapsed" CornerRadius="10" Width="15" Height="15" Margin="0" ToolTipService.ToolTip="Action Pending" d:IsHidden="True" d:IsLocked="True">
        <Border.Background>
            <RadialGradientBrush  GradientOrigin="0.4,0.3" Center="0.4,0.3" RadiusX="0.5" RadiusY="0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="Yellow" Offset="0" />
            <GradientStop Color="Orange" Offset="0.95" />
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
        </Border.Background>
    </Border>
    <Border x:Name="BrdRed" Visibility="Collapsed" CornerRadius="10" Width="15" Height="15" Margin="0" ToolTipService.ToolTip="Stopped" d:IsHidden="True" d:IsLocked="True">
        <Border.Background>
            <RadialGradientBrush  GradientOrigin="0.4,0.3" Center="0.4,0.3" RadiusX="0.5" RadiusY="0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="Orange" Offset="0" />
            <GradientStop Color="Red" Offset="0.95" />
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
        </Border.Background>
    </Border>
    </StackPanel>
</UserControl>

And the code behind for the control: 以及该控件后面的代码:

namespace UI
{
    public partial class MiniTrafficLight : UserControl
    {
        public static readonly DependencyProperty TrafficLightStateProperty = DependencyProperty.Register("TrafficLightState", typeof(TrafficLightState), typeof(MiniTrafficLight), null);

        public TrafficLightState TrafficLightState
    {
        get
        {
            return (TrafficLightState)this.GetValue(TrafficLightStateProperty);
        }
        set
        {
            if (value == TrafficLightState.Red)
        {
            this.BrdRed.Visibility = System.Windows.Visibility.Visible;
            this.BrdGreen.Visibility = System.Windows.Visibility.Collapsed;
            this.BrdYellow.Visibility = System.Windows.Visibility.Collapsed;
        }
        else if (value == TrafficLightState.Yellow)
        {
            this.BrdRed.Visibility = System.Windows.Visibility.Collapsed;
            this.BrdGreen.Visibility = System.Windows.Visibility.Collapsed;
            this.BrdYellow.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
             this.BrdRed.Visibility = System.Windows.Visibility.Collapsed;
                     this.BrdGreen.Visibility = System.Windows.Visibility.Visible;
             this.BrdYellow.Visibility = System.Windows.Visibility.Collapsed;
        }

                this.SetValue(TrafficLightStateProperty, value);
        }
        }
    }
}

so now the problem is that if anywhere on the page I have a grid that displays all traffic light objects using the MiniTraficLight control inside a grid - the MiniTrafficLight control never works... or it never displays the green red or yellow lights. 所以现在的问题是,如果页面上的任何地方都有一个网格,可以使用网格内的MiniTraficLight控件显示所有交通信号灯对象-MiniTrafficLight控件将永远无法工作...或者它永远不会显示绿色的红色或黄色灯光。 I've debugged the service and have verified that the TrafficLightState property is set when its being read from the db for each traffic light object. 我已经调试了该服务,并验证了当从db中为每个交通灯对象读取它时,已设置了TrafficLightState属性。 I also can verify that on the client silverlight app that the TrafficLightState for each TrafficLight object arrives properly (aka... it is set). 我还可以验证在客户端silverlight应用程序上,每个TrafficLight对象的TrafficLightState是否正确到达(aka ...已设置)。

However, if I set the MiniTrafficLight's control TrafficLightState manually without binding it works. 但是,如果我手动设置MiniTrafficLight的控件TrafficLightState而不进行绑定,那么它将起作用。 If I use binding it never shows up. 如果我使用绑定,它将永远不会出现。 With or without a converter that property on the MiniTrafficLight control never gets called. 无论有没有转换器,都不会调用MiniTrafficLight控件上的该属性。 The reason I know is that I added a MessageBox.Show(...) on the set accessor of the property and i don't see any message boxes. 我知道的原因是,我在属性的设置访问器上添加了MessageBox.Show(...) ,但没有看到任何消息框。 If I put break points - they never get reached. 如果我设置了断点-他们永远都无法达到。 Bellow is the sample code that displays the Traffic Light objects inside a grid: 波纹管是在网格内显示交通灯对象的示例代码:

<datagrid:DataGrid x:Name="ReceiverNodesDataGrid" Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True" HorizontalAlignment="Stretch" SelectionMode="Single">
    <datagrid:DataGrid.Columns>
        <datagrid:DataGridTemplateColumn Header="Status">
            <datagrid:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <local:MiniTraficLight TrafficLightState="{Binding TrafficLightState}"/>
        </DataTemplate>
            </datagrid:DataGridTemplateColumn.CellTemplate>
    </datagrid:DataGridTemplateColumn>
        <datagrid:DataGridTextColumn Header="Id" Binding="{Binding Id}" />
    </datagrid:DataGrid.Columns>
</datagrid:DataGrid>

Any idea why this binding doesn't work? 知道为什么这种绑定不起作用吗?

Thanks All, Martin 谢谢,马丁

So apparently the trick to this is to properly implement the dependency properties. 因此,显然,解决这个问题的技巧是正确实现依赖项属性。 UGHHHHH! GH!

namespace UI
{
    public partial class MiniTrafficLight : UserControl
    {
        private static PropertyMetaData trafficLightStateMetaData = new PropertyMetaData(new PropertyChangedCallBack(TrafficLightState_Changed));
        public static readonly DependencyProperty TrafficLightStateProperty = DependencyProperty.Register("TrafficLightState", typeof(TrafficLightState), typeof(MiniTrafficLight), trafficLightStateMetaData);

        public TrafficLightState TrafficLightState
        {
            get { return (TrafficLightState)this.GetValue(TrafficLightStateProperty); }
            set { this.SetValue(TrafficLightStateProperty, value); }
        }

        private static void TrafficLightState_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MiniTrafficLight sender = d as MiniTrafficLight;
            TrafficLightState state = (TrafficLightState)e.NewState;

            if( state == TrafficLightState.Red )
            {
                //...
            }
            else if ( state == TrafficLightState.Yellow )
            {
                //...
            }
            else
            {
                //...
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM