<Window x:Class="WpfTable.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTable"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="Window3" Height="226" Width="483">
<Window.Resources>
<local:ColorConverter1 x:Key="colorConverter" />
</Window.Resources>
<Grid DataContext="{Binding Path=WindowViewModel}">
<StackPanel>
<Grid Name="grid1" DataContext="{Binding Path=Player}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Label Content="FirstName" Grid.Column="0" Grid.Row="0"/>
<Label Content="LastName" Grid.Column="0" Grid.Row="1"/>
<Label Content="PositionName" Grid.Column="0" Grid.Row="2"/>
<TextBox Text="{Binding Path=FirstName}" Grid.Column="1" Grid.Row="0"/>
<TextBox Text="{Binding Path=LastName}" Grid.Column="1" Grid.Row="1"/>
<TextBox Text="{Binding Path=Position}" Grid.Column="1" Grid.Row="2"/>
<TextBlock Name="label1" Text="{Binding Path=FirstName}" Grid.Column="2" Grid.Row="0"/>
<TextBlock Name="label2" Text="{Binding Path=LastName}" Grid.Column="2" Grid.Row="1"/>
<TextBlock Name="label3" Text="{Binding Path=Position}" Grid.Column="2" Grid.Row="2"/>
</Grid>
<StackPanel Orientation="Horizontal">
<Button x:Name="GreenButton" Background="Green" Margin="5,5,5,5" Width="50" Height="20" Click="GreenButton_Click"/>
<Button x:Name="YellowButton" Background="Yellow" Margin="5,5,5,5" Width="50" Height="20" Click="YellowButton_Click"/>
<Button x:Name="RedButton" Background="Red" Margin="5,5,5,5" Width="50" Height="20" Click="RedButton_Click"/>
</StackPanel>
<Grid>
<Border Width="50" Background="Black" CornerRadius="10" BorderBrush="Gray" BorderThickness="2">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="Fill" Value="LightGray" />
<Setter Property="Stroke" Value="Gray" />
<Setter Property="StrokeThickness" Value="2" />
<Setter Property="Margin" Value="4" />
</Style>
</StackPanel.Resources>
<Ellipse Fill="{Binding Green,
Converter={StaticResource colorConverter},
ConverterParameter=GREEN}"/>
<Ellipse Fill="{Binding Yellow,
Converter={StaticResource colorConverter},
ConverterParameter=YELLOW}"/>
<Ellipse Fill="{Binding Red,
Converter={StaticResource colorConverter},
ConverterParameter=RED}"/>
</StackPanel>
</Border>
</Grid>
</StackPanel>
</Grid>
</Window>
namespace WpfTable
{
public partial class Window3 : Window
{
private WindowViewModel _windowViewModel = new WindowViewModel();
public Window3()
{
InitializeComponent();
}
public WindowViewModel WindowViewModel
{
get { return _windowViewModel; }
}
private void RedButton_Click(object sender, RoutedEventArgs e)
{
_windowViewModel.Red = !_windowViewModel.Red;
}
private void YellowButton_Click(object sender, RoutedEventArgs e)
{
_windowViewModel.Yellow = !_windowViewModel.Yellow;
}
private void GreenButton_Click(object sender, RoutedEventArgs e)
{
_windowViewModel.Green = !_windowViewModel.Green;
}
}
public class WindowViewModel : DependencyObject
{
public WindowViewModel()
{
Player = new Player();
Player.FirstName = "Michael";
Player.LastName = "Jordan";
Player.Position = "SG";
}
public static readonly DependencyProperty _redProperty = DependencyProperty.Register("Red", typeof(bool), typeof(WindowViewModel), new PropertyMetadata(false));
public bool Red
{
get { return (bool)GetValue(_redProperty); }
set { SetValue(_redProperty, value); }
}
public static readonly DependencyProperty _yellowProperty = DependencyProperty.Register("Yellow", typeof(bool), typeof(WindowViewModel), new PropertyMetadata(false));
public bool Yellow
{
get { return (bool)GetValue(_yellowProperty); }
set { SetValue(_yellowProperty, value); }
}
public static readonly DependencyProperty _greenProperty = DependencyProperty.Register("Green", typeof(bool), typeof(WindowViewModel), new PropertyMetadata(false));
public bool Green
{
get { return (bool)GetValue(_greenProperty); }
set { SetValue(_greenProperty, value); }
}
public Player Player { get; set; }
}
public class Player : DependencyObject
{
public static DependencyProperty FirstNameProperty =
DependencyProperty.Register("FirstName", typeof(string),
typeof(Player));
public string FirstName
{
get { return (string)GetValue(FirstNameProperty); }
set { SetValue(FirstNameProperty, value); }
}
public static DependencyProperty LastNameProperty =
DependencyProperty.Register("LastName", typeof(string),
typeof(Player));
public string LastName
{
get { return (string)(GetValue(LastNameProperty)); }
set { SetValue(LastNameProperty, value); }
}
public static DependencyProperty PositionProperty =
DependencyProperty.Register("Position", typeof(string),
typeof(Player));
public string Position
{
get { return (string)(GetValue(PositionProperty)); }
set { SetValue(PositionProperty, value); }
}
}
public class ColorConverter1 : IValueConverter
{
public enum Lights
{
GREEN,
YELLOW,
RED
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool state = (bool)value;
Lights light = (Lights)Enum.Parse(typeof(Lights), (string)parameter);
if (state)
{
switch (light)
{
case Lights.GREEN:
return new SolidColorBrush(Colors.Green);
case Lights.YELLOW:
return new SolidColorBrush(Colors.Yellow);
case Lights.RED:
return new SolidColorBrush(Colors.Red);
}
}
return new SolidColorBrush(Colors.White);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}
Comments
Post a Comment