I have inherited a project that displays 2D data in grid. However, things seem to be done in a wrong way. I have set up a minimum working example for a hall and seats:
Data is stored in a model as a 2D array:
public class DataModel
{
public enum SeatState { Empty, Reserved, Sold }
public SeatState[,] Seats;
public DataModel(int rows, int columns)
{
Seats = new SeatState[columns, rows];
}
}
The displaying Window gets reference to the Seats array in constructor, creates Rectangle objects, inserts them into the grid and stores them for later "rendering" use. When the data change, Render method is called, walks through the entire 2D array and sets Rectangle styles.
The XAML with style resources:
<Window x:Class="SE_MWE.HallWindow"
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"
xmlns:local="clr-namespace:WPF_Quora_binding"
mc:Ignorable="d"
Title="HallWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="EmptySeat" TargetType="Rectangle">
<Setter Property="Margin" Value="1" />
<Setter Property="Fill" Value="LightSeaGreen" />
</Style>
<Style x:Key="ReservedSeat" TargetType="Rectangle" BasedOn="{StaticResource EmptySeat}">
<Setter Property="Fill" Value="OrangeRed" />
</Style>
<Style x:Key="SoldSeat" TargetType="Rectangle" BasedOn="{StaticResource EmptySeat}">
<Setter Property="Fill" Value="DarkMagenta" />
</Style>
</Window.Resources>
<Grid x:Name="HallGrid" Margin="10" />
</Window>
And the C# code:
public partial class HallWindow : Window
{
private readonly DataModel.SeatState[,] _seats; //reference to data
private Rectangle[,] _uiSeats; // stored UI elements for rendering
public HallWindow(DataModel.SeatState[,] seats)
{
InitializeComponent();
_seats = seats;
InitializeHall();
RenderHall();
}
private void InitializeHall()
{
_uiSeats = new Rectangle[_seats.GetLength(0), _seats.GetLength(1)];
// define grid rows and columns according to data width and height
for (int i = 0; i < _seats.GetLength(0); i++)
{
HallGrid.ColumnDefinitions.Add(new ColumnDefinition());
}
for (int i = 0; i < _seats.GetLength(1); i++)
{
HallGrid.RowDefinitions.Add(new RowDefinition());
}
// insert child elements, store them for render
for (int x = 0; x < _seats.GetLength(0); x++)
{
for (int y = 0; y < _seats.GetLength(1); y++)
{
Rectangle UISeat = new Rectangle();
HallGrid.Children.Add(UISeat);
Grid.SetRow(UISeat, y);
Grid.SetColumn(UISeat, x);
_uiSeats[x, y] = UISeat;
}
}
}
public void RenderHall()
{
for (int x = 0; x < _seats.GetLength(0); x++)
{
for (int y = 0; y < _seats.GetLength(1); y++)
{
DataModel.SeatState seatState = _seats[x, y];
Rectangle UISeat = _uiSeats[x, y];
UISeat.Style = FindStyle(seatState);
}
}
}
private Style FindStyle(DataModel.SeatState state)
{
switch (state)
{
case DataModel.SeatState.Empty:
return FindResource("EmptySeat") as Style;
case DataModel.SeatState.Reserved:
return FindResource("ReservedSeat") as Style;
case DataModel.SeatState.Sold:
return FindResource("SoldSeat") as Style;
default:
return null;
}
}
}
This way is definitely not the correct MVVM. I know that instead of manually calling Render() method whenever data change, we should only create a binding between the UI elements and the 2D data, and then the framework takes care of all UI updates automatically. I use this regularly with simple dependency properties and bindings written directly into XAML.
However, I'm confused, how to fix this - how to make this kind of binding.
Related
I am trying to achieve effect where each column has its own border, but yet can not find a perfectly working solution.
This kind of look is desired but this is implemented by putting 3 borders in 3 columned Grid, which is not flexible as Grid columns and DataGrid columns are being sized sized separately
<Window x:Class="WpfApp3.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp3" xmlns:usercontrols="clr-namespace:EECC.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Background="LightGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="White" CornerRadius="5" BorderThickness="1" BorderBrush="Black" Margin="5"/>
<Border Background="White" CornerRadius="5" BorderThickness="1" BorderBrush="Black" Margin="5" Grid.Column="1"/>
<Border Background="White" CornerRadius="5" BorderThickness="1" BorderBrush="Black" Margin="5" Grid.Column="2"/>
<DataGrid ItemsSource="{Binding Items}" ColumnWidth="*" AutoGenerateColumns="True" Padding="10" GridLinesVisibility="None" Background="Transparent" Grid.ColumnSpan="3">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="Transparent"/>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent"/>
</Style>
</DataGrid.Resources>
</DataGrid>
</Grid>
This is not trivial if you want to use the DataGrid. The "problem" is that the DataGrid uses a Grid to host the cells. The cell borders are drawn using the feature of the Grid to show grid lines. You can hide the grid lines but still you have the Grid controlling the layout.
Creating the gaps you want should not come easy. The layout system of the Grid makes it an effort to implement a solution that scales well. You can extend the SelectiveScrollingGrid (the hosting panel of the DataGrid) and add the gaps when laying out the items. Knowing about DataGrid internals, I can say it is possible, but not worth the effort.
The alternative solution would be to use a ListView with a GridView as host. The problem here is that the GridView is designed to show rows as single item. You have no chance to modify margins column based. You can only adjust the content. I have not tried to modify the ListView internal layout elements or override the layout algorithm, but in context of alternative solutions I would also rule the ListView using a GridView out - but it is possible. It's not worth the effort.
Solution: Custom View
The simplest solution I can suggest is to adjust the data structure to show data column based. This way you can use a horizontal ListBox. Each item makes a column. Each column is realized as vertical ListBox. You basically have nested ListBox elements.
You would have to take care of the row mapping in order to allow selecting cells of a common row across the vertical ListBox columns.
This can be easily achieved by adding a RowIndex property to the CellItem models.
The idea is to have the horizontal ListBox display a collection of ColumnItem models. Each column item model exposes a collection of CellItem models. The CellItem items of different columns but the same row must share the same CellItem.RowIndex.
As a bonus, this solution is very easy to style. ListBox template has almost no parts compared to the significantly more complex DataGrid or the slightly more complex GridView.
To make showcasing the concept less confusing I chose to implement the grid layout as UserControl. For the sake of simplicity the logic to initialize and host the models and source collections is implemented inside this UserControl. I don't recommend this. Instantiation and hosting of the items should be outside the control e.g., inside a view model. You should add a DependencyProperty as ItemsSource for the control as data source for the internal horizontal ListBox.
Usage Example
<Window>
<ColumnsView />
</Window>
First create the data structure to populate the view.
The structure is based on the type ColumnItem, which hosts a collection of
CellItem items where each CellItem has a CellItem.RowIndex.
The CellItem items of different columns, that logically form a row must share the same CellItem.RowIndex.
ColumnItem.cs
public class ColumnItem
{
public ColumnItem(string header, IEnumerable<CellItem> items)
{
Header = header;
this.Items = new ObservableCollection<CellItem>(items);
}
public CellItem this[int rowIndex]
=> this.Items.FirstOrDefault(cellItem => cellItem.RowIndex.Equals(rowIndex));
public string Header { get; }
public ObservableCollection<CellItem> Items { get; }
}
CellItem.cs
public class CellItem : INotifyPropertyChanged
{
public CellItem(int rowIndex, object value)
{
this.RowIndex = rowIndex;
this.Value = value;
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
=> this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public event PropertyChangedEventHandler PropertyChanged;
public int RowIndex { get; }
private object value;
public object Value
{
get => this.value;
set
{
this.value = value;
OnPropertyChanged();
}
}
private bool isSelected;
public bool IsSelected
{
get => this.isSelected;
set
{
this.isSelected = value;
OnPropertyChanged();
}
}
}
Build and initialize the data structure.
In this example this is all implemented in the UserControl itself with the intend to keep the example as compact as possible.
ColumnsView.xaml.cs
public partial class ColumnsView : UserControl
{
public ColumnsView()
{
InitializeComponent();
this.DataContext = this;
InitializeSourceData();
}
public InitializeSourceData()
{
this.Columns = new ObservableCollection<ColumnItem>();
for (int columnIndex = 0; columnIndex < 3; columnIndex++)
{
var cellItems = new List<CellItem>();
int asciiChar = 65;
for (int rowIndex = 0; rowIndex < 10; rowIndex++)
{
var cellValue = $"CellItem.RowIndex:{rowIndex}, Value: {(char)asciiChar++}";
var cellItem = new CellItem(rowIndex, cellValue);
cellItems.Add(cellItem);
}
var columnHeader = $"Column {columnIndex + 1}";
var columnItem = new ColumnItem(columnHeader, cellItems);
this.Columns.Add(columnItem);
}
}
private void CellsHostListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cellsHost = sender as Selector;
var selectedCell = cellsHost.SelectedItem as CellItem;
SelectCellsOfRow(selectedCell.RowIndex);
}
private void SelectCellsOfRow(int selectedRowIndex)
{
foreach (ColumnItem columnItem in this.Columns)
{
var cellOfRow = columnItem[selectedRowIndex];
cellOfRow.IsSelected = true;
}
}
private void ColumnGripper_DragStarted(object sender, DragStartedEventArgs e)
=> this.DragStartX = Mouse.GetPosition(this).X;
private void ColumnGripper_DragDelta(object sender, DragDeltaEventArgs e)
{
if ((sender as DependencyObject).TryFindVisualParentElement(out ListBoxItem listBoxItem))
{
double currentMousePositionX = Mouse.GetPosition(this).X;
listBoxItem.Width = Math.Max(0 , listBoxItem.ActualWidth - (this.DragStartX - currentMousePositionX));
this.DragStartX = currentMousePositionX;
}
}
public static bool TryFindVisualParentElement<TParent>(DependencyObject child, out TParent resultElement)
where TParent : DependencyObject
{
resultElement = null;
DependencyObject parentElement = VisualTreeHelper.GetParent(child);
if (parentElement is TParent parent)
{
resultElement = parent;
return true;
}
return parentElement != null
? TryFindVisualParentElement(parentElement, out resultElement)
: false;
}
public ObservableCollection<ColumnItem> Columns { get; }
private double DragStartX { get; set; }
}
Create the view using a horizontal ListView that renders it's ColumnItem source collection as a list of vertical ListBox elements.
ColumnsView.xaml
<UserControl x:Class="ColumnsView">
<UserControl.Resources>
<Style x:Key="ColumnGripperStyle"
TargetType="{x:Type Thumb}">
<Setter Property="Margin"
Value="-2,8" />
<Setter Property="Width"
Value="4" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Cursor"
Value="SizeWE" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<!-- Column host. Displays cells of a column. -->
<ListBox ItemsSource="{Binding Columns}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Padding="4"
BorderThickness="1"
BorderBrush="Black"
CornerRadius="8">
<StackPanel>
<TextBlock Text="{Binding Header}" />
<!-- Cell host. Displays cells of a column. -->
<ListBox ItemsSource="{Binding Items}"
BorderThickness="0"
Height="150"
Selector.SelectionChanged="CellsHostListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<!-- Link item container selection to CellItem.IsSelected -->
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Margin" Value="0,0,8,0" /> <!-- Define the column gap -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter />
<Thumb Grid.Column="1"
Style="{StaticResource ColumnGripperStyle}"
DragStarted="ColumnGripper_DragStarted"
DragDelta="ColumnGripper_DragDelta" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</UserControl>
Notes for improvement
The ColumnsView.Columns property should be a DependencyProperty to allow to use the control as binding target.
The column gap can also be a DependencyProperty of ColumnsView.
By replacing the TextBlock that displays the column header with a Button, you can easily add sorting. Having the active column that triggers the sorting e.g. lexically, you would have to sync the other passive columns and sort them based on the CellItem.RowIndex order of the active sorted column.
Maybe choose to extend Control rather than UserControl.
You can implement the CellItem to use a generic type parameter to declare the Cellitem.Value property like CellItem<TValue>.
You can implement the ColumnItem to use a generic type parameter to declare the ColumnItem.Items property like ColumnItem<TColumn>.
Add a ColumnsView.SelectedRow property that returns a collection of all CellItem items of the current selected row
With a fair amount of elbow grease, you can get the desired look by extending the native DataGrid.
Custom header and cell templates should take care of the spacing, with the appropriate background color. The AutoGeneratingColumn behavior requires more control than could easily be achieved in XAML, so I chose to create the templates in code to be able to pass the column's PropertyName.
The observant reader will already have asked themselves: "What about the border at the end of the list?". That's right, we need to be able to distinguish the last item from all others, to be able to template its border differently.
This is done with the following contract:
public interface ICanBeLastItem
{
bool IsLastItem { get; set; }
}
Which the row object needs to implement for the bottom border to be drawn correctly.
This also requires some custom logic when sorting, to update the value of IsLastItem. The pic with the yellow background shows the result of sorting on ThirdNumber.
The native DataGrid provides a Sorting event out of the box, but no Sorted event. The template thingy combined with the need for a custom event, led me to subclass ColumnView from DataGrid instead of declaring it as a UserControl.
I added code-behind to MainWindow, for switching the background color, but that's just for illustration purposes (as I didn't feel like implementing the Command pattern) and has nothing to do with the custom control.
The ColumnView is configured through binding. As always, feel free to extend. The current implementation expects the columns to be auto generated. In either case, the code for generating the templates is provided.
<local:ColumnView ItemsSource="{Binding Items}" Background="LightSteelBlue"/>
Demo code
ColumnView
public class ColumnView : DataGrid
{
public ColumnView()
{
HeadersVisibility = DataGridHeadersVisibility.Column;
HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
// Hidden props from base DataGrid
base.ColumnWidth = new DataGridLength(1, DataGridLengthUnitType.Star);
base.AutoGenerateColumns = true;
base.GridLinesVisibility = DataGridGridLinesVisibility.None;
// Styling
ColumnHeaderStyle = CreateColumnHeaderStyle();
CellStyle = CreateCellStyle(this);
// Event handling
AutoGeneratingColumn += OnAutoGeneratingColumn;
Sorting += OnSorting;
Sorted += OnSorted;
}
#region Hidden props
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new DataGridLength ColumnWidth
{
get => base.ColumnWidth;
set => new InvalidOperationException($"{nameof(ColumnView)} doesn't allow changing {nameof(ColumnWidth)}.");
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new DataGridGridLinesVisibility GridLinesVisibility
{
get => base.GridLinesVisibility;
set => new InvalidOperationException($"{nameof(ColumnView)} doesn't allow changing {nameof(GridLinesVisibility)}.");
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new bool AutoGenerateColumns
{
get => base.AutoGenerateColumns;
set => new InvalidOperationException($"{nameof(ColumnView)} doesn't allow changing {nameof(AutoGenerateColumns)}.");
}
#endregion Hidden props
#region Styling
private static Style CreateColumnHeaderStyle()
=> new Style(typeof(DataGridColumnHeader))
{
Setters =
{
new Setter(BackgroundProperty, Brushes.Transparent),
new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Stretch),
new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch)
}
};
private static Style CreateCellStyle(ColumnView columnView)
=> new Style(typeof(DataGridCell))
{
Setters =
{
new Setter(BorderThicknessProperty, new Thickness(0.0)),
new Setter(BackgroundProperty, new Binding(nameof(Background)) { Source = columnView})
}
};
#endregion Styling
#region AutoGeneratingColumn
// https://stackoverflow.com/questions/25643765/wpf-datagrid-databind-to-datatable-cell-in-celltemplates-datatemplate
private static void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (sender is ColumnView columnView)
{
if (e.PropertyName == nameof(ICanBeLastItem.IsLastItem))
{
e.Cancel = true;
}
else
{
var column = new DataGridTemplateColumn
{
CellTemplate = CreateCustomCellTemplate(e.PropertyName),
Header = e.Column.Header,
HeaderTemplate = CreateCustomHeaderTemplate(columnView, e.PropertyName),
HeaderStringFormat = e.Column.HeaderStringFormat,
SortMemberPath = e.PropertyName
};
e.Column = column;
}
}
}
private static DataTemplate CreateCustomCellTemplate(string path)
{
// Create the data template
var customTemplate = new DataTemplate();
// Set up the wrapping border
var border = new FrameworkElementFactory(typeof(Border));
border.SetValue(BorderBrushProperty, Brushes.Black);
border.SetValue(StyleProperty, new Style(typeof(Border))
{
Triggers =
{
new DataTrigger
{
Binding = new Binding(nameof(DataGridCell.IsSelected)) { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridCell), 1) },
Value = false,
Setters =
{
new Setter(BackgroundProperty, Brushes.White),
}
},
new DataTrigger
{
Binding = new Binding(nameof(DataGridCell.IsSelected)) { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridCell), 1) },
Value = true,
Setters =
{
new Setter(BackgroundProperty, SystemColors.HighlightBrush),
}
},
new DataTrigger
{
Binding = new Binding(nameof(ICanBeLastItem.IsLastItem)),
Value = false,
Setters =
{
new Setter(MarginProperty, new Thickness(5.0, -1.0, 5.0, -1.0)),
new Setter(BorderThicknessProperty, new Thickness(1.0, 0.0, 1.0, 0.0)),
}
},
new DataTrigger
{
Binding = new Binding(nameof(ICanBeLastItem.IsLastItem)),
Value = true,
Setters =
{
new Setter(MarginProperty, new Thickness(5.0, -1.0, 5.0, 0.0)),
new Setter(BorderThicknessProperty, new Thickness(1.0, 0.0, 1.0, 1.0)),
new Setter(Border.CornerRadiusProperty, new CornerRadius(0.0, 0.0, 5.0, 5.0)),
new Setter(Border.PaddingProperty, new Thickness(0.0, 0.0, 0.0, 5.0)),
}
}
}
});
// Set up the TextBlock
var textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetBinding(TextBlock.TextProperty, new Binding(path));
textBlock.SetValue(MarginProperty, new Thickness(10.0, 0.0, 5.0, 0.0));
// Set the visual tree of the data template
border.AppendChild(textBlock);
customTemplate.VisualTree = border;
return customTemplate;
}
private static DataTemplate CreateCustomHeaderTemplate(ColumnView columnView, string propName)
{
// Create the data template
var customTemplate = new DataTemplate();
// Set up the wrapping border
var border = new FrameworkElementFactory(typeof(Border));
border.SetValue(MarginProperty, new Thickness(5.0, 0.0, 5.0, 0.0));
border.SetValue(BackgroundProperty, Brushes.White);
border.SetValue(BorderBrushProperty, Brushes.Black);
border.SetValue(BorderThicknessProperty, new Thickness(1.0, 1.0, 1.0, 0.0));
border.SetValue(Border.CornerRadiusProperty, new CornerRadius(5.0, 5.0, 0.0, 0.0));
// Set up the TextBlock
var textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, propName);
textBlock.SetValue(MarginProperty, new Thickness(5.0));
// Set the visual tree of the data template
border.AppendChild(textBlock);
customTemplate.VisualTree = border;
return customTemplate;
}
#endregion AutoGeneratingColumn
#region Sorting
#region Custom Sorted Event
// https://stackoverflow.com/questions/9571178/datagrid-is-there-no-sorted-event
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent SortedEvent = EventManager.RegisterRoutedEvent(
nameof(Sorted), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ColumnView));
// Provide CLR accessors for the event
public event RoutedEventHandler Sorted
{
add => AddHandler(SortedEvent, value);
remove => RemoveHandler(SortedEvent, value);
}
// This method raises the Sorted event
private void RaiseSortedEvent()
{
var newEventArgs = new RoutedEventArgs(ColumnView.SortedEvent);
RaiseEvent(newEventArgs);
}
protected override void OnSorting(DataGridSortingEventArgs eventArgs)
{
base.OnSorting(eventArgs);
RaiseSortedEvent();
}
#endregion Custom Sorted Event
private static void OnSorting(object sender, DataGridSortingEventArgs e)
{
if (sender is DataGrid dataGrid && dataGrid.HasItems)
{
if (dataGrid.Items[dataGrid.Items.Count - 1] is ICanBeLastItem lastItem)
{
lastItem.IsLastItem = false;
}
}
}
private static void OnSorted(object sender, RoutedEventArgs e)
{
if (sender is DataGrid dataGrid && dataGrid.HasItems)
{
if (dataGrid.Items[dataGrid.Items.Count - 1] is ICanBeLastItem lastItem)
{
lastItem.IsLastItem = true;
}
}
}
#endregion Sorting
}
RowItem
public class RowItem : INotifyPropertyChanged, ICanBeLastItem
{
public RowItem(int firstNumber, string secondNumber, double thirdNumber)
{
FirstNumber = firstNumber;
SecondNumber = secondNumber;
ThirdNumber = thirdNumber;
}
public int FirstNumber { get; }
public string SecondNumber { get; }
public double ThirdNumber { get; }
private bool _isLastItem;
public bool IsLastItem
{
get => _isLastItem;
set
{
_isLastItem = value;
OnPropertyChanged();
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion INotifyPropertyChanged
}
public interface ICanBeLastItem
{
bool IsLastItem { get; set; }
}
MainWindow.xaml
<Window x:Class="WpfApp.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Switch Background" Click="ButtonBase_OnClick" />
<local:ColumnView x:Name="columnView" Grid.Row="1" Padding="10"
ItemsSource="{Binding Items}"
Background="LightSteelBlue"/>
</Grid>
</Window>
MainWindow.xaml.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (columnView.Background == Brushes.LightSteelBlue)
{
columnView.Background = Brushes.DarkRed;
}
else if (columnView.Background == Brushes.DarkRed)
{
columnView.Background = Brushes.Green;
}
else if (columnView.Background == Brushes.Green)
{
columnView.Background = Brushes.Blue;
}
else if (columnView.Background == Brushes.Blue)
{
columnView.Background = Brushes.Yellow;
}
else
{
columnView.Background = Brushes.LightSteelBlue;
}
}
}
MainViewModel
public class MainViewModel
{
public MainViewModel()
{
Items = InitializeItems(200);
}
private ObservableCollection<RowItem> InitializeItems(int numberOfItems)
{
var rowItems = new ObservableCollection<RowItem>();
var random = new Random();
for (var i = 0; i < numberOfItems; i++)
{
var firstNumber = Convert.ToInt32(1000 * random.NextDouble());
var secondNumber = Convert.ToString(Math.Round(1000 * random.NextDouble()));
var thirdNumber = Math.Round(1000 * random.NextDouble());
var rowItem = new RowItem(firstNumber, secondNumber, thirdNumber);
rowItems.Add(rowItem);
}
rowItems[numberOfItems - 1].IsLastItem = true;
return rowItems;
}
public ObservableCollection<RowItem> Items { get; }
}
I used the following links to display my 2 dimensional data in a table:
How to bind an 2D array bool[][] to a WPF DataGrid (one-way)?
Change DataGrid cell colour based on values
All is working except that the background color is not changing (and the converter method is not even being hit). Can someone tell me what's going on?
Below I post a complete, minimal example. I'm not wedded to any of these ideas (using a DataView to bind my IEnumerable> for example) so feel free to suggest alternative methods. My only hard requirement is that in my real project, the data is given as IEnumerable>
Here's the code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel vm = new ViewModel();
List<Column> row1 = new List<Column>()
{
new Column(){Make = Make.Ford,OperatingStatus = OperatingStatus.Broken},
new Column(){Make = Make.Honda, OperatingStatus = OperatingStatus.Unknown}
};
List<Column> row2 = new List<Column>()
{
new Column() {Make = Make.GM, OperatingStatus = OperatingStatus.Working},
new Column() {Make = Make.Toyota, OperatingStatus = OperatingStatus.Broken}
};
List<List<Column>> data = new List<List<Column>>();
data.Add(row1);
data.Add(row2);
vm.Data = data;
DataContext = vm;
}
}
public enum OperatingStatus
{
Working = 0,
Broken = 1,
Unknown = 2
}
public enum Make
{
Ford,
Honda,
GM,
Toyota
}
public class Column
{
public Make Make { get; set; }
public OperatingStatus OperatingStatus { get; set; }
}
public class ViewModel
{
public IEnumerable<IEnumerable<Column>> Data { get; set; }
public DataView MyDataTable
{
get
{
var rows = Data.Count();
var cols = Data.First().Count();
var t = new DataTable();
for (var c = 0; c < cols; c++)
{
t.Columns.Add(new DataColumn(c.ToString()));
}
foreach (var row in Data)
{
var newRow = t.NewRow();
int c = 0;
foreach (var col in row)
{
newRow[c] = col.Make;
c++;
}
t.Rows.Add(newRow);
}
return t.DefaultView;
}
}
}
public class NameToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
switch (input)
{
case "Ford":
return Brushes.LightGreen;
case "GM":
return Brushes.Red;
case "Toyota":
return Brushes.Blue;
case "Honda":
return Brushes.Yellow;
default:
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
And here's the XAML
<Window x:Class="StackOverFlowDataGridQuestion.MainWindow"
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"
xmlns:local="clr-namespace:StackOverFlowDataGridQuestion"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:NameToBrushConverter x:Key="NameToBrushConverter"/>
</Window.Resources>
<Grid>
<ScrollViewer>
<DataGrid Width="1000"
Margin="0"
HorizontalAlignment="Left"
DataContext="{Binding}"
ItemsSource="{Binding MyDataTable}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Make}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Make, Converter={StaticResource NameToBrushConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
After trying Nik's suggestion and replacing 'Make' with 'Row[0]' in two places, I got the following, which is progress because there is some colour! If there is more changes, I'll report back here.
I would have expected to get something where the Ford square is green, Honda is yellow, GM is red, and Toyota is blue. Something more like below (please excuse my horrible markup skills).
That is one of the unfortunate side effect of using a DataView as your ItemsSource. The DataContext of a DataGridRow in this case is a DataRowView which has a property Row. This property contains an array of values which are the individual cells. DataGridCell inherits that DataContext. Then what you're looking for is Row[0] for the first column, Row[1] for your second column and so on. Using the XAML below for the DataGridTextColumn, produced the result you're looking for in my testing, where I used Row[0] instead of Make in the bindings. And thank you for providing such good working code, such a time saver!
<DataGridTextColumn Binding="{Binding Row[0]}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Row[0], Converter={StaticResource NameToBrushConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
I recently needed to do something similar where my input had to be a 2-D array of indeterminate dimensions. I ended up making a reusable Custom Control extending a DataGrid. This control managed its own DataContext (a DataTable), which made the UI nice a clean without the need to use indices or any code-behind.
There may be a nicer way to do this, but I could not figure it out. Plus it really depends on what you're trying to achieve. If your columns are known at design time, I would consider creating an ObservableCollection containing objects. If not, maybe someone has a better trick, but at least this should get your code working.
Here is a solution for posterity. I don't claim it's the best or most elegant and would welcome alternative ideas. In particular, the whole idea of having to expose a DataView rather than just IEnumerable<IEnumerable<Column>> seems crazy.
In additions to the articles I initially mentioned, I also found the following extremely useful:
https://codefornothing.wordpress.com/2009/01/25/the-wpf-datagrid-and-me/
https://social.msdn.microsoft.com/Forums/vstudio/en-US/b3cbe382-99b0-4005-8cb9-cd2f36e74ed3/how-to-change-a-datagrid-cells-background-color-using-a-converter?forum=wpf
CODE
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel vm = new ViewModel();
List<Column> row1 = new List<Column>()
{
new Column(){Make = Make.Ford,OperatingStatus = OperatingStatus.Broken},
new Column(){Make = Make.Honda, OperatingStatus = OperatingStatus.Unknown}
};
List<Column> row2 = new List<Column>()
{
new Column() {Make = Make.GM, OperatingStatus = OperatingStatus.Working},
new Column() {Make = Make.Toyota, OperatingStatus = OperatingStatus.Broken}
};
List<List<Column>> data = new List<List<Column>>();
data.Add(row1);
data.Add(row2);
vm.Data = data;
DataContext = vm;
}
}
public enum OperatingStatus
{
Working = 0,
Broken = 1,
Unknown = 2
}
public enum Make
{
Ford,
Honda,
GM,
Toyota
}
public class Column
{
public Make Make { get; set; }
public OperatingStatus OperatingStatus { get; set; }
}
public class ViewModel
{
public IEnumerable<IEnumerable<Column>> Data { get; set; }
public DataView MyDataTable
{
get
{
var rows = Data.Count();
var cols = Data.First().Count();
var t = new DataTable();
for (var c = 0; c < cols; c++)
{
t.Columns.Add(new DataColumn(c.ToString()));
//t.Columns.Add(new DataColumn(c.ToString(),typeof(StackOverFlowDataGridQuestion.Column)));
}
foreach (var row in Data)
{
var newRow = t.NewRow();
int c = 0;
foreach (var col in row)
{
newRow[c] = col.Make;
c++;
}
t.Rows.Add(newRow);
}
return t.DefaultView;
}
}
}
public class ConverterHoldoffGridColor : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[1] is DataRow)
{
var cell = (DataGridCell) values[0];
var row = (DataRow) values[1];
var columnName = cell.Column.SortMemberPath;
string input = (row[columnName] as string);
//string input = (row[columnName] as StackOverFlowDataGridQuestion.Column).Make.ToString();
switch (input)
{
case "Ford":
return Brushes.LightGreen;
case "GM":
return Brushes.Red;
case "Toyota":
return Brushes.Blue;
case "Honda":
return Brushes.Yellow;
default:
return DependencyProperty.UnsetValue;
}
}
else
{
return SystemColors.AppWorkspaceColor;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And here's the XAML
<Window x:Class="StackOverFlowDataGridQuestion.MainWindow"
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"
xmlns:local="clr-namespace:StackOverFlowDataGridQuestion"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:ConverterHoldoffGridColor x:Key="bgHoldoffGridColor" />
<Style x:Key="CellHighlighterStyle">
<Setter Property="DataGridCell.Background">
<Setter.Value>
<MultiBinding
Converter="{StaticResource bgHoldoffGridColor}" >
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"/>
<Binding Path="Row" Mode="OneWay"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ScrollViewer>
<DataGrid x:Name="myDataGrid" CellStyle="{StaticResource CellHighlighterStyle}"
DataContext="{Binding }"
ItemsSource="{Binding MyDataTable}">
</DataGrid>
</ScrollViewer>
</Grid>
Is there such a thing as a multi-state togglebutton in silverlight? I've tried retemplating radio buttons in a group, but I'd like to make a storyboard that works across the toggle states and because radio buttons are separate objects I'm stuck(I'm trying to make it look like the iphone toggle button, except multi-state)
The standard Silverlight CheckBox as an IsThreeState property. The default visual for the third state is "-", so you might have to customise the visuals.
A CheckBox is a specialisation of a ToggleButton.
You probably know of OOTB checkbox control:
http://samples.msdn.microsoft.com/Silverlight/SampleBrowser/#/?sref=System.Windows.Controls.CheckBoxEx
That was probably useful, but for one of my projects, it had to be similar functionality but using an input box instead so I used custom UserControl approach. I'd highly recommend it.
In the constructor of usercontrol, after InitializeComponent(); you can specify the values that affect the UI. Time permitting I'll annotate some xaml+cs.
Here's some code:
XAML:
<UserControl x:Class="SilverlightApplication1.MainPage"
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="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<Style x:Key="TernaryTB" TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Width" Value="64"/>
<Setter Property="Height" Value="39"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
<Setter Property="FontFamily" Value="Portable User Interface"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="TextAlignment" Value="Center" />
</Style>
</Grid.Resources>
<TextBox x:Name="BackgroundTextBox" IsEnabled="True" BorderBrush="White" Foreground="{x:Null}" Visibility="Visible" Style="{StaticResource TernaryTB}"/>
<TextBox x:Name="TernaryTextBox" Style="{StaticResource TernaryTB}" GotFocus="TernaryTextBox_GotFocus"/>
</Grid>
</UserControl>
CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
private int current;
public int Current
{
get
{
return current;
}
set
{
current = value;
}
}
public struct DataStructure
{
private string label;
private Color color;
private Color forecolor;
public DataStructure(string label, Color c, Color c2)
{
this.label = label;
this.color = c;
this.forecolor = c2;
}
public string Label
{
get { return label; }
set { label = value; }
}
public Color Color
{
get { return color; }
set { color = value; }
}
public Color Forecolor
{
get { return forecolor; }
set { forecolor = value; }
}
}
IList<DataStructure> DataStructureArray;
public MainPage()
{
InitializeComponent();
current = 0;
int i = 0;
DataStructureArray = new Collection<DataStructure>
(new[] {
new DataStructure ("A", Colors.Red,Colors.Green),
new DataStructure ("B", Colors.Green,Colors.Blue),
new DataStructure ("C", Colors.Blue,Colors.Red)
});
foreach (DataStructure listItem in DataStructureArray)
{
if (i == current)
{
TernaryTextBox.Text = listItem.Label;
TernaryTextBox.Background = new SolidColorBrush(listItem.Color);
break;
}
i++;
}
}
public MainPage(IList<DataStructure> NewDataStructureArray)
{
int i = 0;
DataStructureArray = new Collection<DataStructure>();
foreach (DataStructure ds in NewDataStructureArray)
{
DataStructureArray.Add(new DataStructure(ds.Label, ds.Color, ds.Forecolor));
i++;
}
}
private void TernaryTextBox_GotFocus(object sender, RoutedEventArgs e)
{
int i = 0;
current = (current + 1) % 3;
foreach (DataStructure listItem in DataStructureArray)
{
if (i == current)
{
TernaryTextBox.Text = listItem.Label;
TernaryTextBox.Background = new SolidColorBrush(listItem.Color);
TernaryTextBox.Foreground = new SolidColorBrush(listItem.Forecolor);
BackgroundTextBox.Focus();
break;
}
i++;
}
BackgroundTextBox.Focus();
}
}
}
To test, simply add a brand spanking usercontrol to your project, copy paste my code; to test my code in isolation, open App.xaml.cs and simply replace:
this.RootVisual = new MainPage();
with:
this.RootVisual = new <Whatevernameyouchosefortheusercontrol>();
to see it in action
Does anyone knows any good tool for drawing correlation heat maps for WPF?
Example based on comments:
Original image source
The free WPF Toolkit has a TreeMap. You can define it in XAML as follows:
<vis:TreeMap ItemsSource="{Binding Path=HeatMap.Sectors}"
Interpolators="{StaticResource colourInterpolator}">
<vis:TreeMap.ItemDefinition>
<vis:TreeMapItemDefinition ValueBinding="{Binding MarketCap}">
<DataTemplate>
<Grid>
<Border x:Name="Border"
BorderBrush="Black"
BorderThickness="1"
Margin="1"
Opacity="0.5">
</Border>
<TextBlock Text="{Binding Name}"
TextWrapping="Wrap"
FontSize="20"
Margin="5"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
</vis:TreeMapItemDefinition>
</vis:TreeMap.ItemDefinition>
</vis:TreeMap>
The above XAML is a snippet from an application I have written that shows financial HeatMaps. You can see a Silverlight version running here:
http://www.scottlogic.co.uk/blog/colin/xaml-finance/
(Just hit the 'heatmap' button)
If you are looking for a commercial product, I would suggest you look at the Telerik controls. Telerik has excellent controls for WPF. Included in the long list is a Heat Map control. Here is a link to the site where they list the heat map feature:
http://www.telerik.com/products/wpf/map.aspx
If you are looking to build something, here are a couple blog articles that lay out how to do it (with source provided):
http://www.garrettgirod.com/?p=111
http://www.nickdarnell.com/?p=833
The Syncfusion charting component appears to provide heatmaps.
Not a free component, but if you can get your hands on the Telerik library you could use the following:
http://www.telerik.com/products/wpf/heatmap.aspx
I have had to use it in the past for a few projects and it worked pretty well.
I used DevExpress with a custom ColorFormatter behaviour. I couldn't find anything on the market that did this out of the box. This took me a few days to develop. My code attaached below, hopefully this helps someone out there.
Edit: I used POCO view models and MVVM however you could change this to not use POCO if you desire.
Table2DViewModel.cs
namespace ViewModel
{
[POCOViewModel]
public class Table2DViewModel
{
public ITable2DView Table2DView { get; set; }
public DataTable ItemsTable { get; set; }
public Table2DViewModel()
{
}
public Table2DViewModel(MainViewModel mainViewModel, ITable2DView table2DView) : base(mainViewModel)
{
Table2DView = table2DView;
CreateTable();
}
private void CreateTable()
{
var dt = new DataTable();
var xAxisStrings = new string[]{"X1","X2","X3"};
var yAxisStrings = new string[]{"Y1","Y2","Y3"};
//TODO determine your min, max number for your colours
var minValue = 0;
var maxValue = 100;
Table2DView.SetColorFormatter(minValue,maxValue, null);
//Add the columns
dt.Columns.Add(" ", typeof(string));
foreach (var x in xAxisStrings) dt.Columns.Add(x, typeof(double));
//Add all the values
double z = 0;
for (var y = 0; y < yAxisStrings.Length; y++)
{
var dr = dt.NewRow();
dr[" "] = yAxisStrings[y];
for (var x = 0; x < xAxisStrings.Length; x++)
{
//TODO put your actual values here!
dr[xAxisStrings[x]] = z++; //Add a random values
}
dt.Rows.Add(dr);
}
ItemsTable = dt;
}
public static Table2DViewModel Create(MainViewModel mainViewModel, ITable2DView table2DView)
{
var factory = ViewModelSource.Factory((MainViewModel mainVm, ITable2DView view) => new Table2DViewModel(mainVm, view));
return factory(mainViewModel, table2DView);
}
}
}
ITable2DView.cs
namespace Interfaces
{
public interface ITable2DView
{
void SetColorFormatter(float minValue, float maxValue, ColorScaleFormat colorScaleFormat);
}
}
Table2DView.xaml.cs
namespace View
{
public partial class Table2DView : ITable2DView
{
public Table2DView()
{
InitializeComponent();
}
static ColorScaleFormat defaultColorScaleFormat = new ColorScaleFormat
{
ColorMin = (Color)ColorConverter.ConvertFromString("#FFF8696B"),
ColorMiddle = (Color)ColorConverter.ConvertFromString("#FFFFEB84"),
ColorMax = (Color)ColorConverter.ConvertFromString("#FF63BE7B")
};
public void SetColorFormatter(float minValue, float maxValue, ColorScaleFormat colorScaleFormat = null)
{
if (colorScaleFormat == null) colorScaleFormat = defaultColorScaleFormat;
ConditionBehavior.MinValue = minValue;
ConditionBehavior.MaxValue = maxValue;
ConditionBehavior.ColorScaleFormat = colorScaleFormat;
}
}
}
DynamicConditionBehavior.cs
namespace Behaviors
{
public class DynamicConditionBehavior : Behavior<GridControl>
{
GridControl Grid => AssociatedObject;
protected override void OnAttached()
{
base.OnAttached();
Grid.ItemsSourceChanged += OnItemsSourceChanged;
}
protected override void OnDetaching()
{
Grid.ItemsSourceChanged -= OnItemsSourceChanged;
base.OnDetaching();
}
public ColorScaleFormat ColorScaleFormat { get; set;}
public float MinValue { get; set; }
public float MaxValue { get; set; }
private void OnItemsSourceChanged(object sender, EventArgs e)
{
var view = Grid.View as TableView;
if (view == null) return;
view.FormatConditions.Clear();
foreach (var col in Grid.Columns)
{
view.FormatConditions.Add(new ColorScaleFormatCondition
{
MinValue = MinValue,
MaxValue = MaxValue,
FieldName = col.FieldName,
Format = ColorScaleFormat,
});
}
}
}
}
Table2DView.xaml
<UserControl x:Class="View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:ViewModels="clr-namespace:ViewModel"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:behaviors="clr-namespace:Behaviors"
xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
DataContext="{dxmvvm:ViewModelSource Type={x:Type ViewModels:ViewModel}}"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="800">
<UserControl.Resources>
<Style TargetType="{x:Type dxg:GridColumn}">
<Setter Property="Width" Value="50"/>
<Setter Property="HorizontalHeaderContentAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type dxg:HeaderItemsControl}">
<Setter Property="FontWeight" Value="DemiBold"/>
</Style>
</UserControl.Resources>
<!--<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="" Command="{Binding OnLoadedCommand}"/>
</dxmvvm:Interaction.Behaviors>-->
<dxg:GridControl ItemsSource="{Binding ItemsTable}"
AutoGenerateColumns="AddNew"
EnableSmartColumnsGeneration="True">
<dxmvvm:Interaction.Behaviors >
<behaviors:DynamicConditionBehavior x:Name="ConditionBehavior" />
</dxmvvm:Interaction.Behaviors>
<dxg:GridControl.View>
<dxg:TableView ShowGroupPanel="False"
AllowPerPixelScrolling="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
</UserControl>
I want to increase or decrease font size of controls such as window, treeView, ribbon menu etc that are contained by main window.
I have a font size slider create method and I want to acces all of Control and TextBlock by using visualtree helper and increase or decrease their font size according to slider value.
Methods are below;
private StackPanel CreateFontSizeSlider()
{
StackPanel fontSizePanel = new StackPanel();
fontSizePanel.Orientation = Orientation.Horizontal;
Slider fontSizeSlider = new Slider();
fontSizeSlider.Minimum = -3;
fontSizeSlider.Maximum = 5;
fontSizeSlider.Value = 0;
fontSizeSlider.Orientation = Orientation.Horizontal;
fontSizeSlider.TickPlacement = System.Windows.Controls.Primitives.TickPlacement.TopLeft;
fontSizeSlider.IsSnapToTickEnabled = true;
fontSizeSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(fontSizeSlider_ValueChanged);
fontSizeSlider.Width = 150;
fontSizePanel.Children.Add(fontSizeSlider);
return fontSizePanel;
}
public static void ChangeControlsFontSize(DependencyObject dependencyObject, double value)
{
foreach (DependencyObject childItem in GetChildren(dependencyObject))
{
if (childItem is Control)
{
Control control = childItem as Control;
control.FontSize = control.FontSize + value;
}
else if (childItem is TextBlock)
{
((TextBlock)childItem).FontSize = ((TextBlock)childItem).FontSize + value;
}
ChangeControlsFontSize(childItem, value);
}
}
private static IEnumerable<DependencyObject> GetChildren(DependencyObject reference)
{
int childCount = VisualTreeHelper.GetChildrenCount(reference);
for (int i = 0; i < childCount; i++)
{
yield return VisualTreeHelper.GetChild(reference, i);
}
}
private void fontSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ChangeControlsFontSize(this, e.NewValue - e.OldValue);
}
There are some problems;
Firstly I can not acces all controls by walking visual tree. For example I cannot acces closed ribbon menu items. So I can not change their fonts.
Secondly some controls contain another controls so I increase or decrease control font size twice.
Is there any solution for these proplems or is there another way to do this ? Could you help me please ?
You are working way too hard. :-D
Create a style like this:
<Style TargetType="ListBox">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize">
<Setter.Value>
<Binding Source="{x:Static Application.Current}" Path="fontSize"/>
</Setter.Value>
</Setter>
</Style>
Create a property called fontSize on your application.
Make a slider like this:
<Slider Name="fontSize" Minimum="10" Maximum="22" IsSnapToTickEnabled="True" TickPlacement="TopLeft"
Value="{Binding Source={x:Static Application.Current}, Path=fontSize, Mode=TwoWay}"/>
Now, any control with that style will nicely resize - and no code is needed!