绑定到静态属性问题

本文关键字:问题 属性 静态 绑定 | 更新日期: 2023-09-27 18:05:30

我正在使用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}"

任何帮助都是感激的。

谢谢。

绑定到静态属性问题

您是否可以使用d:ItemsSource="{x:Null}"之类的东西,并在您的窗口或UserControl中定义d,如mc:Ignorable="d" ?

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

编辑

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