我有一个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
}
因此,鉴于WCF RIA服务,我需要将此交通灯对象提供给Silverlight客户端应用程序:
public IEnumerable<TrafficLight> GetTrafficLightsForCity(int cityId)
{
return Data.GetTrafficLightsForCity(cityId).AsEnumerable();
}
这样可行。我得到了我的清单,一切都很好,直到我决定将列表绑定到一个将显示我的交通信号灯的网格...(PS。这是一个类似sim城市的游戏):
交通信号灯在网格中的显示方式是通过一个显示灯光状态的小控件 - &gt;红色,黄色,绿色,加上其他一些东西。现在我将向您展示Light状态和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>
控制背后的代码:
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);
}
}
}
}
所以现在问题是,如果页面上的任何地方我有一个网格,使用网格内的MiniTraficLight控件显示所有红绿灯对象 - MiniTrafficLight控件永远不会工作......或者它永远不会显示绿色的红色或黄色的灯光。我调试了该服务,并验证了从每个红绿灯对象的数据库中读取TrafficLightState属性时的设置。我还可以在客户端silverlight应用程序上验证每个TrafficLight对象的TrafficLightState是否正确到达(也就是......它已设置)。
但是,如果我手动设置MiniTrafficLight的控件TrafficLightState而不进行绑定则可以正常工作。如果我使用绑定它永远不会出现。有或没有转换器,MiniTrafficLight控件上的属性永远不会被调用。我知道的原因是我在属性的set访问器上添加了MessageBox.Show(...)
,我没有看到任何消息框。如果我提出破发点 - 他们永远不会达成。 Bellow是显示网格内交通灯对象的示例代码:
<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>
知道为什么这种绑定不起作用?
全部谢谢, 马丁
答案 0 :(得分:0)
显然,这样做的诀窍是正确实现依赖属性。 UGHHHHH!
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
{
//...
}
}
}
}