您的位置:首页 > 其它

Silverlight数据绑定转换示例

2012-02-12 00:19 363 查看
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<local:ColorConverter x:Key="ColorConverter"></local:ColorConverter>
</Grid.Resources>

<Ellipse Width="300" Height="200" Fill="{Binding Status,Converter={StaticResource ColorConverter},Mode=TwoWay}"></Ellipse>

</Grid>
public enum TrafficStatus
{
Stop,Ready,Go
}

public class TrafficLight : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public TrafficStatus Status
{
get { return status; }
set
{
status = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("status"));
}
}
}

private TrafficStatus status;
}

public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
TrafficStatus status = (TrafficStatus)value;
SolidColorBrush brush = new SolidColorBrush(Colors.Red);
switch (status)
{
case TrafficStatus.Stop:
break;
case TrafficStatus.Ready:
brush = new SolidColorBrush(Colors.Orange);
break;
case TrafficStatus.Go:
brush = new SolidColorBrush(Colors.Green);
break;
default:
break;
}
return brush;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
TrafficLight traffic = new TrafficLight()
{
Status = TrafficStatus.Stop
};
this.DataContext = traffic;
}
Silverlight数据绑定转换.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: