WPF Telerik Scatter Point colors - c#

I am beginner in WPF telerik technology framework.
I want custom colors to points in telerik scatter point chart. But colors of points are not changing. What should I do? Is there something fundamentally wrong in what I am doing?
Here is c# code
var pallate = ColorPallate();
var PS = new ScatterPointSeries();
var pallaet = new ChartPalette();
PaletteEntryCollection pall = new PaletteEntryCollection();
for (int i = 0; i < data.GetLength(0); i++)
{
ScatterDataPoint point = new ScatterDataPoint();
point.XValue = data[i, XAxisIndex];
point.YValue = data[i, YAaxisIndex];
int value = 0;
if (!float.IsNaN(PredictedResult[i]))
value = System.Convert.ToInt32(PredictedResult[i]);
var filler = new PaletteEntry();
filler.fill(pallate[value]);
pall.Add(filler);
PS.DataPoints.Add(point);
}
pallaet.SeriesEntries.Add(pall);
this.Cross_Plot.Palette = pallaet;
Where ColorPallate() provide custom color palatte as
private Brush[] ColorPallate()
{
var pallate = new Brush[15];
pallate[0] = new SolidColorBrush(Colors.Red);
pallate[1] = new SolidColorBrush(Colors.Orange);
pallate[2] = new SolidColorBrush(Colors.Green);
pallate[3] = new SolidColorBrush(Colors.Pink);
pallate[4] = new SolidColorBrush(Colors.Black);
pallate[5] = new SolidColorBrush(Colors.Brown);
pallate[6] = new SolidColorBrush(Colors.Crimson);
pallate[7] = new SolidColorBrush(Colors.DarkOrange);
pallate[8] = new SolidColorBrush(Colors.ForestGreen);
pallate[9] = new SolidColorBrush(Colors.Indigo);
pallate[10] = new SolidColorBrush(Colors.DarkKhaki);
pallate[11] = new SolidColorBrush(Colors.Purple);
pallate[12] = new SolidColorBrush(Colors.Gold);
pallate[13] = new SolidColorBrush(Colors.RosyBrown);
pallate[14] = new SolidColorBrush(Colors.Gray);
return pallate;
}
Here is XMAL code
<telerik:RadCartesianChart x:Name="Cross_Plot" Margin="0,51,17,0" VerticalAlignment="Top" Height="472" Grid.Column="2" HorizontalAlignment="Right" Width="1057" Grid.RowSpan="2" >
<telerik:RadCartesianChart.HorizontalAxis>
<telerik:LinearAxis SmartLabelsMode="SmartStep" MajorTickOffset="0"/>
</telerik:RadCartesianChart.HorizontalAxis>
<telerik:RadCartesianChart.VerticalAxis>
<telerik:LinearAxis SmartLabelsMode="SmartStep" ElementBrush="Black" />
</telerik:RadCartesianChart.VerticalAxis>
<telerik:ScatterPointSeries XValueBinding="XValue" YValueBinding="YValue" ItemsSource="{Binding}">
<telerik:ScatterPointSeries.DefaultVisualStyle>
<Style TargetType="Path">
<Setter Property="Fill" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.DataItem.Color}" />
<Setter Property="Width" Value="10" />
<Setter Property="Height" Value="10" />
</Style>
</telerik:ScatterPointSeries.DefaultVisualStyle>
</telerik:ScatterPointSeries>
</telerik:RadCartesianChart>
<TextBlock Grid.Column="2" HorizontalAlignment="Left" Margin="131,37,0,0" TextWrapping="Wrap" Text="Well points on X Axis" VerticalAlignment="Top" Width="248"/>
<TextBlock Grid.Column="2" HorizontalAlignment="Left" Margin="389,35,0,0" TextWrapping="Wrap" Text="Well points on Y axis" VerticalAlignment="Top" Width="243"/>
I shall be very thankful to you for your efforts

You could use Telerik's PointTemplateSelector. This allows each point to be customized dynamically in code.

Related

Change dot for comma in chart labels Charting c#

i have to change the decimal separator of the labels of my chart. I can't put them directly like string because they need a doublé to make the chart.
Here is the code:
private static byte[] ObtenerBarraDoble(IList<ValorBarraDTO> valores)
{
var newColor1 = Color.FromArgb(187, 189, 191);
var newColor2 = Color.FromArgb(0, 138, 209);
using (var graficoPie = new Chart { Height = 200, Width = 700, RenderType = RenderType.BinaryStreaming })
{
var chartAreaPie = new ChartArea();
//chartAreaPie.AxisX.LabelStyle.Format = "dd/MMM\nhh:mm";
chartAreaPie.AxisX.MajorGrid.LineColor = Color.White;
chartAreaPie.AxisY.MajorGrid.LineColor = Color.White;
chartAreaPie.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8f);
chartAreaPie.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8f);
chartAreaPie.AxisX.LabelStyle.Format = "N0";
chartAreaPie.AxisY.LabelStyle.Format = "N0";
graficoPie.ChartAreas.Add(chartAreaPie);
var serieNuevo = new Series("Cartera Actual")
{
ChartType = SeriesChartType.Column,
XValueMember = "label",
YValueMembers = "valor1",
Color = newColor1,
Legend = "Cartera Actual",
IsValueShownAsLabel = true,
LabelFormat = "N1",
CustomProperties = "LabelStyle=Top"
};
graficoPie.Series.Add(serieNuevo);
var serie = new Series("Cartera Recomendada")
{
ChartType = SeriesChartType.Column,
XValueMember = "label",
YValueMembers = "valor2",
Color = newColor2,
Legend = "Cartera Propuesta",
IsValueShownAsLabel = true,
LabelFormat = "N1",
CustomProperties = "LabelStyle=Top"
};
graficoPie.Series.Add(serie);
graficoPie.DataSource = valores;
return PdfHelper.ChartABinario(graficoPie);
}
}
I think maybe in CustomProperties? i need to do this, please help!
Can you tell me if you are using XAML? The key is the template style formatting. This is how i did some formatting. If it doesn't help, I apologize for wasting your time.
<Style x:Key="SeriesColumn" TargetType="DVC:ColumnDataPoint">
<Setter Property="Background" Value="{Binding BackColor}"></Setter>
<Setter Property="Foreground" Value="{Binding ForeColor}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DVC:ColumnDataPoint">
<Grid>
<Rectangle
Fill="{TemplateBinding Background}"
Stroke="Black"
StrokeThickness="1"
/>
<Grid
Background="Transparent"
Margin="{Binding SeriesMargin}"
HorizontalAlignment="Stretch"
VerticalAlignment="Top">
<TextBlock
HorizontalAlignment="Center"
Background="Transparent"
Foreground="{TemplateBinding Foreground}"
Text="{TemplateBinding FormattedDependentValue}"
FontWeight="Bold"
FontSize="12"
/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The key is the Template and the TetBlock. if think it's possible to go deep enough to be able to replace , with a .

Scale child back in WPF

I have a Grid which scaled/zoomed with ScaleTransform by slider. At runtime many UIElements are added to this Grid.
I want to show some tooltips, but not scaled! How should I do that?
For the example: Grid has scaleX and scaleY 2, so I set new ScaleTransform(0.5, 0.5), but didn't help. It seems that the most similar value is 0.740.. Why?
Even Grid's LayoutTransform.Inverse is set to scale values 0.5.
XAML:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="Auto" Width="Auto" Name="graphScrollViewer" ScrollChanged="graphScrollViewer_ScrollChanged">
<Grid Margin="0,0,0,0" Name="graphGrid" Width="Auto" Height="Auto" ScrollViewer.IsDeferredScrollingEnabled="True" MouseLeftButtonDown="graphGrid_MouseLeftButtonDown" MouseLeftButtonUp="graphGrid_MouseLeftButtonUp" MouseMove="graphGrid_MouseMove">
<Grid.LayoutTransform>
<ScaleTransform ScaleX="{Binding ElementName=sldZoom, Path=Value}" ScaleY="{Binding ElementName=sldZoom, Path=Value}" />
</Grid.LayoutTransform>
</Grid>
</ScrollViewer>
<Slider Minimum="0.1" Maximum="20" Value="1" x:Name="sldZoom" Panel.ZIndex="10" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,20,20" Height="23" Width="100" ValueChanged="sldZoom_ValueChanged"/>
Code-behind:
(method of Rectangle (MouseEnter event) dynamically added to grid)
private void rect_MouseEnter(object sender, MouseEventArgs e)
{
RectToolTip = new TextBlock();
RectToolTip.HorizontalAlignment = HorizontalAlignment.Left;
RectToolTip.VerticalAlignment = VerticalAlignment.Top;
RectToolTip.TextAlignment = TextAlignment.Center;
RectToolTip.Height = this.HeaderTwoHeight + 1;
RectToolTip.Text = " " + (RectsTasks[(sender as Rectangle)]).Info + " ";
RectToolTip.Background = this.ToolTipBackground;
RectToolTip.Foreground = this.ToolTipFontColor;
RectToolTipBorder = new Border();
RectToolTipBorder.Child = RectToolTip;
RectToolTipBorder.BorderThickness = new Thickness(this.ToolTipBorderThickness);
RectToolTipBorder.BorderBrush = this.ToolTipBorderColor;
RectToolTipBorder.Margin = new Thickness(e.GetPosition((graphGrid)).X + 10, e.GetPosition((graphGrid)).Y + 10, 0, 0);
RectToolTipBorder.VerticalAlignment = System.Windows.VerticalAlignment.Top;
RectToolTipBorder.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
graphGrid.Children.Add(RectToolTipBorder);
RectToolTipBorder.LayoutTransform = RectToolTip.LayoutTransform = new ScaleTransform(????);
Grid.SetZIndex(RectToolTip, 20);
Grid.SetZIndex(RectToolTipBorder, 20);
}
You need to assign the inverse transform to the child element, so that the child will stay intact.
RectToolTipBorder.LayoutTransform = graphGrid.LayoutTransform.Inverse as Transform;

Why UserControl without XAML controls do not inherit properties?

I had a working user control done in WPF in XAML. Now I have removed the XAML and created the usercontrol with code because I need to inherit from this control. Previosuly I had code like this one on the code using the control:
<mc:MyControl Foreground="White">
And all the controls inside the MyControl control used this Foreground setting. Now that I've created the control just using C# this is not happening anymore.
Someone know why and how to fix this?
Thanks in advance.
EDIT:
This is the XAML:
<UserControl x:Class="Utils.Wpf.Chart.Chart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
SizeChanged="UserControl_SizeChanged">
<Grid>
<Border Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Canvas Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2" x:Name="DrawArea" Canvas.Background="Transparent" MouseEnter="DrawArea_MouseEnter" MouseLeave="DrawArea_MouseLeave" MouseMove="DrawArea_MouseMove" Cursor="None">
<Line x:Name="Border1" X1="0" X2="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" Y1="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" StrokeThickness="1" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
<Line x:Name="Border2" Y1="0" Y2="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}" X1="0" X2="0" StrokeThickness="1" Stroke="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
<TextBlock x:Name="CrossValue" Text="[-,-]" Foreground="#EBDE11" Visibility="Hidden"/>
</Canvas>
<TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MaxYCaption, TargetNullValue=MaxY}" Margin="3,0,6,0"/>
<TextBlock Grid.Column="0" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MinYCaption, TargetNullValue=MinY}" Margin="3,0,6,0"/>
<TextBlock Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MinX, TargetNullValue=MinX}" Margin="3,3,0,0"/>
<TextBlock Grid.Column="2" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Top" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MaxX, TargetNullValue=MaxX}" Margin="0,3,3,0"/>
</Grid>
</Border>
</Grid>
</UserControl>
This is the C#:
_outerGrid = new Grid();
_border = new Border();
_innerGrid = new Grid();
_canvas = new Canvas();
_line1 = new Line();
_line2 = new Line();
_cursorText = new TextBlock();
_maxXText = new TextBlock();
_maxYText = new TextBlock();
_minXText = new TextBlock();
_minYText = new TextBlock();
this.Content = _outerGrid;
_border.Margin = new Thickness(5);
_border.HorizontalAlignment = HorizontalAlignment.Stretch;
_border.VerticalAlignment = VerticalAlignment.Stretch;
_outerGrid.Children.Add(_border);
_column1 = new ColumnDefinition();
_column1.Width = new GridLength(0, GridUnitType.Auto);
_column2 = new ColumnDefinition();
_column2.Width = new GridLength(1, GridUnitType.Star);
_column3 = new ColumnDefinition();
_column3.Width = new GridLength(1, GridUnitType.Star);
_row1 = new RowDefinition();
_row1.Height = new GridLength(1, GridUnitType.Star);
_row2 = new RowDefinition();
_row2.Height = new GridLength(1, GridUnitType.Star);
_row3 = new RowDefinition();
_row3.Height = new GridLength(0, GridUnitType.Auto);
_innerGrid.ColumnDefinitions.Add(_column1);
_innerGrid.ColumnDefinitions.Add(_column2);
_innerGrid.ColumnDefinitions.Add(_column3);
_innerGrid.RowDefinitions.Add(_row1);
_innerGrid.RowDefinitions.Add(_row2);
_innerGrid.RowDefinitions.Add(_row3);
_border.Child = _innerGrid;
_canvas.Background = Brushes.Transparent;
_canvas.Cursor = Cursors.None;
_canvas.MouseEnter += new MouseEventHandler(_canvas_MouseEnter);
_canvas.MouseMove += new MouseEventHandler(_canvas_MouseMove);
_canvas.MouseLeave += new MouseEventHandler(_canvas_MouseLeave);
Grid.SetColumn(_canvas,1);
Grid.SetColumnSpan(_canvas,2);
Grid.SetRowSpan(_canvas,2);
_innerGrid.Children.Add(_canvas);
_line1.X1 = 0;
_line1.StrokeThickness = 1;
Binding x2Binding = new Binding("ActualWidth");
x2Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) {AncestorType = _canvas.GetType()};
_line1.SetBinding(Line.X2Property, x2Binding);
Binding yBinding = new Binding("ActualHeight");
yBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = _canvas.GetType() };
_line1.SetBinding(Line.Y1Property, yBinding);
_line1.SetBinding(Line.Y2Property, yBinding);
_line2.Y1 = 0;
_line2.X1 = 0;
_line2.X2 = 0;
_line2.StrokeThickness = 1;
Binding y2Binding = new Binding("ActualHeight");
y2Binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = _canvas.GetType() };
_line2.SetBinding(Line.Y2Property, x2Binding);
Binding strokeBinding = new Binding("BorderBrush");
strokeBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
_line2.SetBinding(Shape.StrokeProperty, strokeBinding);
_cursorText.Text = "[-,-]";
_cursorText.Foreground = new SolidColorBrush(Color.FromRgb(0xeb, 0xde, 11));
_cursorText.Visibility = Visibility.Hidden;
_canvas.Children.Add(_line1);
_canvas.Children.Add(_line2);
_canvas.Children.Add(_cursorText);
_maxYText.VerticalAlignment = VerticalAlignment.Top;
_maxYText.HorizontalAlignment = HorizontalAlignment.Right;
_maxYText.Margin = new Thickness(3, 0, 6, 0);
Binding maxYBinding = new Binding("MaxYCaption");
maxYBinding.TargetNullValue = "MaxY";
maxYBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
_maxYText.SetBinding(TextBlock.TextProperty, maxYBinding);
_minYText.VerticalAlignment = VerticalAlignment.Bottom;
_minYText.HorizontalAlignment = HorizontalAlignment.Right;
_minYText.Margin = new Thickness(3, 0, 6, 0);
Binding minYBinding = new Binding("MinYCaption");
minYBinding.TargetNullValue = "MinY";
minYBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
_minYText.SetBinding(TextBlock.TextProperty, minYBinding);
_maxXText.VerticalAlignment = VerticalAlignment.Top;
_maxXText.HorizontalAlignment = HorizontalAlignment.Right;
_maxXText.Margin = new Thickness(0, 3, 3, 0);
Binding maxXBinding = new Binding("MaxX");
maxXBinding.TargetNullValue = "MaxX";
maxXBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
_maxXText.SetBinding(TextBlock.TextProperty, maxXBinding);
_minXText.VerticalAlignment = VerticalAlignment.Top;
_minXText.HorizontalAlignment = HorizontalAlignment.Left;
_minXText.Margin = new Thickness(3, 3, 0, 0);
Binding minXBinding = new Binding("MinX");
minXBinding.TargetNullValue = "MinX";
minXBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(UserControl) };
_minXText.SetBinding(TextBlock.TextProperty, minXBinding);
_innerGrid.Children.Add(_maxYText);
_innerGrid.Children.Add(_maxXText);
_innerGrid.Children.Add(_minYText);
_innerGrid.Children.Add(_minXText);
Grid.SetColumn(_maxXText, 2);
Grid.SetColumn(_minXText, 1);
Grid.SetRow(_minYText, 1);
Grid.SetRow(_minXText, 2);
Grid.SetRow(_maxXText, 2);
this.SizeChanged += new System.Windows.SizeChangedEventHandler(Chart2_SizeChanged);
#SoMoS - I think what you are talking about is a CustomControl, and not a UserControl. If so, did you apply a control template to your inherited control?
You can read about the differences between custom controls to user controls in here-
http://wangmo.wordpress.com/2007/09/28/user-controls-vs-custom-controls/
http://www.wpftutorial.net/CustomVsUserControl.html
If you will apply a control template to your custom control, you could manage its look just as you would with a User Control with XAML.

Bug in C# code equivalent of Silverlight xaml

Why does the following C# code not produce the equivalent of my Silverlight code?
XAML
<Border CornerRadius="8" BorderBrush="White" Height="70" BorderThickness="4">
<StackPanel Orientation="Horizontal">
<Border Margin="5,0,0,0" BorderBrush="White" Height="45" Width="45" BorderThickness="2" CornerRadius="2" Background="White">
<Image Source="/Crystal%20Cloud;component/Resources/Images/weapons/swords/sword_0.png" />
</Border>
<StackPanel Margin="10,0,0,0">
<TextBlock Text="Wooden Dagger" FontFamily="Comic Sans MS" />
<TextBlock Text="DPS: 1" FontFamily="Comic Sans MS" FontSize="16" Margin="15,0,0,0" />
</StackPanel>
</StackPanel>
</Border>
C#
private Border CreateListItem(Item item)
{
// Main border
Border itemBorder = new Border();
itemBorder.BorderThickness = new Thickness(4);
itemBorder.CornerRadius = new CornerRadius(8);
itemBorder.BorderBrush = new SolidColorBrush(Colors.White);
itemBorder.Height = 70;
// Main stack panel
StackPanel mainPanel = new StackPanel();
mainPanel.Orientation = Orientation.Horizontal;
itemBorder.Child = mainPanel;
// The item's image border
Border imageBorder = new Border();
imageBorder.Margin = new Thickness(5, 0, 0, 0);
itemBorder.BorderThickness = new Thickness(2);
itemBorder.CornerRadius = new CornerRadius(2);
itemBorder.BorderBrush = new SolidColorBrush(Colors.White);
itemBorder.Background = new SolidColorBrush(Colors.White);
itemBorder.Height = 45;
itemBorder.Width = 45;
mainPanel.Children.Add(imageBorder);
// The item's image
Image image = new Image();
image.Source = new BitmapImage(new Uri("/Crystal%20Cloud;component/Resources/Images/weapons/swords/sword_0.png"));
imageBorder.Child = image;
// The stack panel for the text
StackPanel textPanel = new StackPanel();
textPanel.Margin = new Thickness(10, 0, 0, 0);
mainPanel.Children.Add(textPanel);
// The title text block
TextBlock titleText = new TextBlock();
titleText.Text = "Wooden Dagger";
titleText.FontFamily = new FontFamily("Comic Sans MS");
textPanel.Children.Add(titleText);
// The status text block
TextBlock statusText = new TextBlock();
statusText.Text = "DPS: 1";
statusText.FontFamily = new FontFamily("Comic Sans MS");
statusText.FontSize = 16;
statusText.Margin = new Thickness(15, 0, 0, 0);
textPanel.Children.Add(statusText);
return itemBorder;
}
Erg... just found the bug itemBorder should be changed to imageBorder.

convert this UniformGrid xaml language to C#

Hi everyone i want to convert this xaml code to c# code
please help me using a looping to save memory or line
please help me
<UniformGrid Width="500" Height="500" x:Name="ChessBoard" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid x:Name="Grid1" Background="Yellow" />
<Grid x:Name="Grid2" Background="Black" />
<Grid x:Name="Grid3" Background="Yellow" />
<Grid x:Name="Grid4" Background="Black" />
<Grid x:Name="Grid5" Background="Yellow" />
<Grid x:Name="Grid6" Background="Black" />
<Grid x:Name="Grid7" Background="Yellow" />
<Grid x:Name="Grid8" Background="Black" />
<Grid x:Name="Grid9" Background="Yellow" />
</UniformGrid>
please
UniformGrid ChessBoard = new UniformGrid();
ChessBoard.Width = 500;
ChessBoard.Height = 500;
ChessBoard.HorizontalAlignment = HorizontalAlignment.Center;
ChessBoard.VerticalAlignment = VerticalAlignment.Center;
Grid chressBox = new Grid();
SolidColorBrush yell = new SolidColorBrush(Colors.Yellow);
SolidColorBrush blk = new SolidColorBrush(Colors.Black);
for (int ii = 1; ii <= 9; ii++)
{
if (ii % 2 == 0)
{
chressBox.Background = blk;
chressBox.Name = "Grid" + ii.ToString();
ChessBoard.Children.Add(chressBox);
}
else
{
chressBox.Background = yell;
chressBox.Name = "Grid" + ii.ToString();
ChessBoard.Children.Add(chressBox);
}
}
LayoutRoot.Children.Add(ChessBoard);
itry to create that, but still wrong
For a full chessboard you could do something like this
Brush defaultBrush = new SolidColorBrush(Colors.Yellow);
Brush alternateBrush = new SolidColorBrush(Colors.Black);
for (int x = 0; x < 8; ++x)
{
for (int y = 0; y < 8; ++y)
{
Grid cell = new Grid();
cell.Background = (y+x) % 2 == 0 ? defaultBrush : alternateBrush;
ChessBoard.Children.Add(cell);
}
}
Here is a version with only a single for loop
Brush defaultBrush = new SolidColorBrush(Colors.Yellow);
Brush alternateBrush = new SolidColorBrush(Colors.Black);
for (int i = 0; i < 64; ++i)
{
Grid cell = new Grid();
cell.Background = (i + i / 8) % 2 == 0 ? defaultBrush : alternateBrush;
ChessBoard.Children.Add(cell);
}
This code assumes you have a ChessBoard defined as a UniformGrid as in your example.

Categories