将用户选择的值附加到ComboBox中的预设字符串

时间:2015-01-20 15:09:52

标签: c# wpf xaml

好的,所以我认为这是一个快速而简单的过程,但我一直在努力找到解决这个问题的方法。

我想要做的是有一个下拉菜单,显示"行:(值)"默认情况下。打开下拉列表时,我希望它仅显示可用值。

这是我的C#代码:

private void RowsComboBox_Loaded(object sender, RoutedEventArgs e)
    {

        List<int> data = new List<int>();
        data.Add(2);
        data.Add(3);
        data.Add(4);
        data.Add(5);
        data.Add(6);
        data.Add(7);
        data.Add(8);

        var comboBox = sender as ComboBox;
        comboBox.ItemsSource = data;
        comboBox.SelectedIndex = 0;
}

private void RowsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var comboBox = sender as ComboBox;
        string value = comboBox.SelectedItem.ToString();
}

XAML:

<ComboBox x:Name="RowsComboBox"  Loaded="RowsComboBox_Loaded" SelectionChanged="RowsComboBox_SelectionChanged"/>

现在我可以让值改变,但我只能显示文本或值,但不能同时显示两者。 我可以做一些工作,但这让我感到恼火,我只想看看它是如何完成的。

3 个答案:

答案 0 :(得分:1)

我认为你真的不想在组合框控件中使用“Rows:”文本

<StackPanel Orientation="Horizontal">
    <Label x:Name="RowsLabel" Content="Rows:"/>
    <ComboBox x:Name="RowsComboBox"/>
</StackPanel>

StackPanel通过在您的组合中并排放置一个静态标签“Rows:”来完成您想要的任务。

如果您正在寻找,请告诉我。

答案 1 :(得分:0)

您需要使用ItemTemplates来执行此操作: 看看:https://meleak.wordpress.com/2012/05/13/different-combobox-itemtemplate-for-dropdown/

它做你想要的,但反过来。

希望这会对你有所帮助。但请记住,对于你想要的简单事物来说,它是相当大的:D

答案 2 :(得分:0)

我发现显示&#34;行的问题:&#34; ComboBox中的用户选择可以类似于John Castleman使用StackPanel描述的方式来解决

<ComboBox x:Name="cbRows" Loaded="Rows_Loaded" SelectionChanged="Rows_SelectionChanged" Width="Auto" Height="Auto" ItemContainerStyle="{StaticResource DropDownItemStyle}">
    <ComboBox.ItemTemplate>
        <DataTemplate >
            <StackPanel Orientation="Horizontal" ToolTipService.ShowOnDisabled="True" Background="Transparent" Margin="0,1.5,0,1.5">
                <TextBlock Text="Rows: " Width="Auto" Height="Auto" Margin="5,0,5,0"/>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" Text="{Binding}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

正如您所看到的,我还使用了ItemContainerStyle来确保下拉菜单仅在展开ComboBox时显示可选值。我使用的风格是:

<Style TargetType="{x:Type ComboBoxItem}" x:Key="DropDownItemStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Grid SnapsToDevicePixels="true" Width="Auto" Height="Auto">
                    <Border x:Name="Border" CornerRadius="2,2,2,2">
                        <StackPanel HorizontalAlignment="Stretch" Width="Auto" Height="Auto">
                            <TextBlock Text="{Binding}" HorizontalAlignment="Stretch" Width="Auto" Height="Auto" VerticalAlignment="Center" x:Name="TextBlock"/>
                        </StackPanel>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter Property="Background" TargetName="Border" Value="#FF3399FF"/>
                        <Setter Property="Foreground" TargetName="TextBlock" Value="White"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>