Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set d:DataContext d:DesignInstance on DataGrid

Tags:

.net

wpf

xaml

The xaml:

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBox VerticalAlignment="Center" FocusManager.IsFocusScope="True" Grid.ColumnSpan="2" Name="SearchTextBox" Text="{Binding Path=SearchTerm.Value, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource FontStyle}" KeyUp="SearchTextBoxKeyUp" />

    <DataGrid Grid.Row="1" ItemsSource="{Binding Path=EpisodesView, Mode=TwoWay}" MouseDoubleClick="ListViewDoubleClick" Style="{StaticResource FontStyle}" KeyUp="ListViewKeyup" Name="EpisodeList" AutoGenerateColumns="False" IsReadOnly="True"
              d:DataContext="{d:DesignInstance vm:Episode}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="File" Binding="{Binding File.Name}" />
            <DataGridTextColumn Header="Age" Binding="{Binding PrettyAge}" SortMemberPath="Age" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</Window>

In the DataGrid at the bottom of the xaml above I am unable to BOTH get the design time d:DataContext DesignInstance references to be correct for both the binding of the DataGrid itself and the bindings of the specific columns.

If I include the the following snippet in DataGrid (as the xaml above) the DataGridTextColumns works, and I get intellisense for the column bindings:

d:DataContext="{d:DesignInstance vm:Episode}

But now the DataGrid binding intellisense no longer work. If I remove the snippet above the reverse happens: Intellisense for DataGrid binding works, but the column bindings don't.

To make it clear, this is only a design time problem. Everything works fine runtime.

like image 836
Andreas Presthammer Avatar asked Jul 30 '12 15:07

Andreas Presthammer


1 Answers

Setting the d:DataContext property changes the design time to use a new Episode object created using the default constructor. You may need to set IsDesignTimeCreatable to true

d:DataContext="{d:DesignInstance vm:Episode, d:IsDesignTimeCreatable=True}"

However I expect your issue is that Episode is the item type and does not have a property EpisodesView. You will need to set the data grid design time data context to a collection of Episodes

like image 159
bstoney Avatar answered Oct 20 '22 16:10

bstoney