I am working on my first MVVM application and I am having problems with it working correctly.
In my main window, I have a button that executes an SQL command that returns an object like a data table.
The window also contains a user control consisting of column headers and Windows HostGridView forms. I need to somehow tell the user control to execute a method that passes data to the DataGridView so that it can update its values.
I tried to create a dependency property in my WPF grid control bound to the data in my view model, but it is not updating properly.
How can I make this work?
- EDIT -
Here is the XAML for my LiteGrid user control -
<UserControl x:Class="ReportUtility.Controls.LiteGrid.LiteGrid" 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:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" xmlns:lite="clr-namespace:ReportUtility.Controls.LiteGrid" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <ScrollViewer x:Name="_scroll" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden"> <ItemsControl ItemsSource="{Binding Columns}" Grid.Row="0" Background="AliceBlue"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </ScrollViewer> <WindowsFormsHost Background="White" Grid.Row="1"> <lite:LiteGridView x:Name="_liteGridView"/> </WindowsFormsHost> </Grid>
My main view model:
public class MainWindowViewModel : DependencyObject { private readonly ILiteTableSource _source; public ICommand ExecuteQueryCommand { get; set; } public LiteGridViewModel Grid { get; set; } public string SqlCommandText { get; set; } public MainWindowViewModel(ILiteTableSource source) { this.ExecuteQueryCommand = new ExecuteQueryCommand(this); _source = source; _source.DataArrived+=new Action<DataSources.LiteSource.LiteTable>(_source_DataArrived); } public void ExecuteQuery() { _source.Connection = new ServerConnection(); _source.CommandText = this.SqlCommandText; _source.ExecuteQuery(); } public LiteTable Results { get { return (LiteTable)GetValue(ResultsProperty); } set { SetValue(ResultsProperty, value); } }
And XAML:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="50"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Button Grid.Row="0" Content="TestButton" HorizontalAlignment="Stretch" Command="{Binding ExecuteQueryCommand}"/> <TextBox Grid.Row="1" Text="{Binding Path=SqlCommandText, UpdateSourceTrigger=PropertyChanged}"/> <lite:LiteGrid Grid.Row="2" Data="{Binding Data, UpdateSourceTrigger=PropertyChanged}"/> </Grid>
source share