binding to static property problem
I'm using the EntityFramework
and working on an invoicing application. Below is the xaml code from myDataGrid
. Now it compiles and everything works great, but the problem is, the static binding I have on the DataGridComboBoxColumn
throws an exception when in designer mode because that static property actually loads from a MySql
database using the entity framework.
<DataGrid x:Name="ItemsGrid" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding Items}" FontSize="11" AutoGenerateColumns="False" CanUserAddRows="False" RowHeaderWidth="0" GridLinesVisibility="None" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="150" Header="Item No.">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="130" BorderThickness="0" Background="Transparent" Text="{Binding Number}" />
<Image MouseDown="ItemSelectionButton_Click" Margin="0,0,0,0" Width="12" Source="/Images/Icons/SearchBlack.png" />
</StackPanel>
</Data开发者_运维技巧Template>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="70" Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Width="70" Header="Order" Binding="{Binding Order}" />
<DataGridTextColumn Width="70" Header="B/O" Binding="{Binding BackOrder}" />
<DataGridTextColumn Width="60" Header="Units" Binding="{Binding Units}" />
<DataGridTextColumn Width="200" Header="Description" Binding="{Binding Description}" />
<DataGridTextColumn Width="90" Header="Price" Binding="{Binding Price}" />
<DataGridComboBoxColumn Width="50" Header="Tax" ItemsSource="{x:Static app:Session.TaxCodes}" SelectedValueBinding="{Binding TaxCodeID}" DisplayMemberPath="Code" SelectedValuePath="ID" />
<DataGridTextColumn Width="90" Header="Amount" Binding="{Binding Amount}" />
<DataGridTextColumn Width="90" Header="Linked" Binding="{Binding SalesOrderID}" />
</DataGrid.Columns>
</DataGrid>
Here's the exception:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at DtcInvoicer.Models.DatabaseEntities..ctor() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\DatabaseModel.Designer.cs:line 42
at DtcInvoicer.Models.Repository.get_GetContext() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\Repository.cs:line 14
at DtcInvoicer.Models.TaxCodesRepository.SelectAll() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Models\TaxCodesRepository.cs:line 54
at DtcInvoicer.Session..cctor() in F:\Development\DTC Industrial\DtcInvoicer\DtcInvoicer\Session.cs:line 18
I know trying to fix the root of the problem will be too much work, I'm wondering if there's a simple workaround like dynamically binding to a static property in another class. Anyone have any idea how to do this? Looking for a xaml
only solution.
Here's what I already tried...
ItemsSource="{Binding Path=TaxCodes, Source={x:Static app:Session}}"
ItemsSource="{Binding Path=TaxCodes, Source={StaticResource app:Session}}"
ItemsSource="{Binding app:Session.TaxCodes}"
Any help is appreciated.
Thanks.
Could you use something like d:ItemsSource="{x:Null}"
, and define d
in your Window or UserControl like mc:Ignorable="d"
?
It should make the design-time property of ItemsSource
equal to null
Edit
Forgot to add the definition for mc
- It is
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
精彩评论