绑定到静态属性问题

时间:2011-08-12 16:08:23

标签: c# wpf xaml data-binding

我正在使用EntityFramework并正在使用发票应用程序。以下是我DataGrid的xaml代码。现在它编译,一切都很好,但问题是,我在DataGridComboBoxColumn上的静态绑定在设计器模式下引发异常,因为静态属性实际上是使用实体框架从MySql数据库加载的

    <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>
                    </DataTemplate>
                </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>

以下是例外:

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

我知道尝试修复问题的根源将是太多的工作,我想知道是否有一个简单的解决方法,如动态绑定到另一个类中的静态属性。任何人都知道如何做到这一点?寻找仅xaml解决方案。

这是我已经尝试过的......

ItemsSource="{Binding Path=TaxCodes, Source={x:Static app:Session}}"

ItemsSource="{Binding Path=TaxCodes, Source={StaticResource app:Session}}"

ItemsSource="{Binding app:Session.TaxCodes}"

感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:2)

您可以使用d:ItemsSource="{x:Null}"之类的内容,并在您的Window或UserControl中定义d,例如mc:Ignorable="d"吗?

它应该使ItemsSource的设计时属性等于null

修改

忘记添加mc的定义 - 它是 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"