I have a problem with WPF and MVVM. I created a Sudokufield consisting of 3 classes:
- SudokuFieldViewModel: The whole playing field (consists of 3*3 SudokuFieldBlockViewModels)
- SudokuBlockViewModel: One block of 3*3 SodokuBoxViewModels
- SudokuBoxViewModel: A box where a number between 1 and 9 can be entered
The SudokuBoxViewModel has a IsHighlighted-property of type bool.
I created a method Highlight in the SudokuFieldViewModel that calls a Highlight-method on every SudokuBlockViewModel which then sets IsHighlighted to true for all the SudokuBoxViewModels.
The property in BoxViewModel looks like this:
private bool m_IsHighlighted = false;
public bool IsHighlighted
{
get
{
return m_IsHighlighted;
}
set
{
if (m_IsHighlighted != value)
{
m_IsHighlighted = value;
RaisePropertyChanged("IsHighlighted");
}
}
}
The views are layed out in a way that FieldView has an ItemsControl with DataTemplates binding to the BlockViewModels in the FieldViewModel. The same concept applies to BoxViewModels in the BlockViewModel.
SudokuBlock:
<UserControl x:Class="SuSo.SudokuBlock"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<SuSo_Common:HighlightConverter x:Key="HighlightConverter"/>
<DataTemplate x:Key="BlockBoxTemplate">
<Grid>
<local:SudokuBox DataContext="{Binding}" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<Border BorderThickness="1" BorderBrush="Black">
<ItemsControl ItemsSource="{Binding Elements, Mode=OneWay}" ItemTemplate="{DynamicResource BlockBoxTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid
Rows="{Binding Size, Mode=OneWay}"
Columns="{Binding Size, Mode=OneWay}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</UserControl>
SudokuBox:
<UserControl x:Class="SuSo.SudokuBox"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<SuSo_Common:HighlightConverter x:Key="HighlightConverter"/>
<DataTemplate x:Key="NumberTemplate" DataType="int">
<Grid>
<Viewbox Stretch="Fill" d:LayoutOverrides="Width, Height" >
<TextBlock x:Name="possibleNumber" Tex开发者_运维百科t="{Binding Mode=OneWay}" FontFamily="Hobo Std" TextAlignment="Center" Height="Auto" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="{Binding ActualHeight, ElementName=possibleNumber, Mode=OneWay}" />
</Viewbox>
</Grid>
</DataTemplate>
</UserControl.Resources>
<UserControl.DataContext>
<local:SudokuBoxViewModel Size="3" />
</UserControl.DataContext>
<Border BorderThickness="1" BorderBrush="Black" Background="{Binding IsHighlighted, Converter={StaticResource HighlightConverter}}">
<ItemsControl ItemsSource="{Binding Elements, Mode=OneWay}" ItemTemplate="{DynamicResource NumberTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid
Rows="{Binding Size, Mode=OneWay}"
Columns="{Binding Size, Mode=OneWay}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</UserControl>
The BoxView binds to IsHighlighted and uses a ValueConverter to convert the bool value to a SolidColorBrush that is used as background for the BoxView.
The problem however is that the GUI doesn't update when I call the Highlight function.
What could be the problem?
Oh my god!
The lines
<UserControl.DataContext>
<local:SudokuBoxViewModel Size="3" />
</UserControl.DataContext>
were the problem (it becomes pretty obvious now..).
I removed this and now it works as it should :).
精彩评论