我如何为wpf中组合框的选定项目编写项目模板

发布于 2024-11-15 05:16:15 字数 1110 浏览 5 评论 0原文

我已经为 wpf 组合框创建了一个项目模板,以显示组合框项目中的多个属性和对象。

<ComboBox x:Name="cmbType" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="1"
        SelectedItem="{Binding Current.Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEditable="False"
        ItemsSource="{Binding Types, Mode=OneWay}" DropDownClosed="cmbIncidentType_DropDownClosed" TabIndex="601">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="4" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding ID}" />
                <TextBlock Grid.Column="2" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

到目前为止,它很完美,它在组合框项目中显示了类型的 ID 和名称,但现在我希望当我选择一个项目时,它应该只在组合框的文本区域中显示所选项目的名称,而不是同时显示 ID 和名称。

有人可以帮忙吗?

i have created an item template for wpf combo box to display multiple properties of and object in a combo box item.

<ComboBox x:Name="cmbType" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="1"
        SelectedItem="{Binding Current.Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEditable="False"
        ItemsSource="{Binding Types, Mode=OneWay}" DropDownClosed="cmbIncidentType_DropDownClosed" TabIndex="601">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="4" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding ID}" />
                <TextBlock Grid.Column="2" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

till now its perfect it shows both id and name of a type in combo box item but now i want when i select an item it should only display name of selected item in the text area of combo box instead of displaying both ID and name.

Can anyone please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2024-11-22 05:16:15

您可以使用触发器来隐藏 ComboBox 显示区域中的 ID:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ID}" Margin="0,0,4,0">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}, Path=IsSelected}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <TextBlock Text="{Binding Name}"/>
    </StackPanel>
</DataTemplate>

You can use a trigger to hide the ID in the ComboBox display area:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ID}" Margin="0,0,4,0">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}, Path=IsSelected}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <TextBlock Text="{Binding Name}"/>
    </StackPanel>
</DataTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文