Reputation: 297
First off all, my View and ViewModel are connected correctly. But I can't get through binding in DataTemplate.
So, I have DataTemplate
in Windows.Resources
:
<Window.Resources>
<DataTemplate x:Key="DataGridHeader">
<DockPanel>
<TextBlock DockPanel.Dock="Top" TextAlignment="Left" Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" Margin="5"/>
<TextBox DockPanel.Dock="Top" BorderBrush="#3d3d3d" BorderThickness="1" Margin="5" Height="22">
</TextBox>
<Button DockPanel.Dock="Top" Height="30" Command="{Binding DataContext.OnFilterCommand, RelativeSource={RelativeSource AncestorType=Window}}"></Button>
</DockPanel>
</DataTemplate>
</Window.Resources>
and then, I have my DataGrid
:
<DataGrid x:Name="DataGridItems"
Grid.Row="1"
EnableRowVirtualization="True"
ItemsSource="{Binding Items, Mode=TwoWay}"
RowHeight="25"
RowHeaderWidth="0"
VerticalContentAlignment="Center"
HorizontalGridLinesBrush="#dddddd"
VerticalGridLinesBrush="#dddddd"
AlternatingRowBackground="#f2f2f2"
AutoGenerateColumns="False"
Background="#cecece"
CanUserAddRows="False">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Padding" Value="0" />
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="#111111"></Setter>
<Setter Property="Background" Value="#cecece" />
<Setter Property="BorderBrush" Value="#111111" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="SeparatorVisibility" Value="Visible"></Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" HeaderTemplate="{StaticResource DataGridHeader}" Width="65" />
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" HeaderTemplate="{StaticResource DataGridHeader}" Width="150" />
</DataGrid.Columns>
</DataGrid>
in my ViewModel
I have:
private ICommand _onLoadedCommand;
public ICommand OnFilterCommand => _onLoadedCommand ?? (_onLoadedCommand = new DelegateCommand(OnFilter));
private void OnFilter()
{
Debug.WriteLine("Works!");
}
and it doesn't work! I looked every where. I looked at this article: http://nosalan.blogspot.com/2013/10/wpf-datagrid-with-filtering-mvvm.html
and it looks to me that I'm doing everything correct.
What am I missing?
Upvotes: 1
Views: 194
Reputation: 7325
You are missing nothing. I have tested your code and it works! The command will be executed. The only difference - i have used not a DelegateCommand, but my implementation, so i don't think, that it is a reason. Restart VS, reboot computer. It should work!
Upvotes: 1