【问题标题】:Binding Combobox to display integer values绑定组合框以显示整数值
【发布时间】:2013-01-07 04:22:18
【问题描述】:

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

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

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

但 ComboBox 只显示 SQL char (C# string) 值,其他为空:

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>

【问题讨论】:

    标签: c# wpf binding combobox converter


    【解决方案1】:

    我认为您在区分大小写方面存在问题。绑定时,您必须完全重写变量的名称。

    试试这个:

    <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"

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2011-11-27
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多