c# - 绑定(bind)组合框以显示整数值

标签 c# wpf binding combobox converters

我将组合框绑定(bind)到实体。 我希望组合框在每个项目上显示不同格式的多个值(整数、字符串和日期时间值),如下所示:

Item#1) 100  - Description - 01/01/2013

Item#2) 101  - Description - 01/01/2013

但是ComboBox只显示SQL char(C#字符串)值,其他都是空的:

Item#1)     - Description -

Item#2)     - Description - 

我必须使用转换器,我是否走错了路,或者有更简单的解决方案?

在 XAML 中

 <UserControl.Resources>
    <CollectionViewSource x:Key="tSCHEDEViewSource" d:DesignSource="{d:DesignInstance my:TSCHEDE, CreateList=True}" />
    <DataTemplate x:Key="SchedaTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=KSCHEDA}" Width="60"></TextBlock>
            <TextBlock Text="{Binding Path=DArticolo}" Width="200"></TextBlock>
            <TextBlock Text=" - " Width="40"></TextBlock>
            <TextBlock Text="{Binding Path=DStorico}" Width="150"></TextBlock>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

<ComboBox ItemTemplate="{StaticResource SchedaTemplate}" Grid.Column="1" Grid.Row="1" Height="23"   HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="23,129,0,0" Name="tSCHEDEComboBox1" SelectedValuePath="KScheda" VerticalAlignment="Top" Width="393">
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>
    </ComboBox>

.edmx 模型

<EntityType Name="TSCHEDE">
      <Key>
        <PropertyRef Name="KSCHEDA" />
      </Key>
      <Property Name="KSCHEDA" Type="int" Nullable="false" />
      <Property Name="KLINEA" Type="int" Nullable="false" />
      <Property Name="DSCHEDA" Type="char" MaxLength="30" />
      <Property Name="DSTORICO" Type="datetime" />
      <Property Name="FINSMAN" Type="char" MaxLength="1" />
      <Property Name="DNOTE" Type="char" MaxLength="255" />
      <Property Name="FCANC" Type="char" MaxLength="1" />
      <Property Name="DArticolo" Type="char" MaxLength="60" />
      <Property Name="FFIGLIA" Type="char" MaxLength="1" />
    </EntityType>

最佳答案

我认为您有区分大小写的问题。绑定(bind)时,您必须准确重写变量的名称。

试试这个:

<UserControl.Resources>
    <CollectionViewSource x:Key="tSCHEDEViewSource" d:DesignSource="{d:DesignInstance my:TSCHEDE, CreateList=True}" />
    <DataTemplate x:Key="SchedaTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=KSCHEDA}" Width="60"></TextBlock>
            <TextBlock Text="{Binding Path=DARTICOLO" Width="200"></TextBlock>
            <TextBlock Text=" - " Width="40"></TextBlock>
            <TextBlock Text="{Binding Path=DSTORICO}" Width="150"></TextBlock>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

SelectedValuePath 中,您也有错误的变量,请将其更改为 SelectedValuePath="KSCHEDA"

关于c# - 绑定(bind)组合框以显示整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14483947/

相关文章:

c# - Xamarin.Forms ListView 绑定(bind)问题

wpf - 到 ScaleTransform 的模板绑定(bind)在自定义控件中不起作用

macos - Cocoa OS X 绑定(bind)和重要数据模型

wpf静态绑定(bind)问题

c# - MySql长时间运行的查询在Docker .NET Core上失败:尝试读取流的末尾/预期读取4个 header 字节,但仅收到0

c# - 如何根据多个条件使用 Linq 查找特定项目?

c++ - 在 WPF 中托管 native 控件

c# - .Net 不可用 "Extension Property"之类的功能

java - JSF 在绑定(bind)时抛出参数类型不匹配

c# - 我怎样才能 catch 404?