Windows 10 Universal App中标记为ComboBox [英] Labeled ComboBox in Windows 10 Universal App

查看:70
本文介绍了Windows 10 Universal App中标记为ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于我的标签文本框,问题在以下位置解决:

similar to my Labeled TextBox, which issues are resolved in:

Windows Universal App中标记的文本框

我在标签组合框中遇到了两个问题,但首先出现了代码:

I got two issues in my Labeled Combobox, but first the Code:

Generic.xaml

<Style TargetType="template:LabeledComboBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="template:LabeledComboBox">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="{TemplateBinding Label}" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0" />
                    <ComboBox x:Name="PART_ComboBox" ItemsSource="{TemplateBinding ItemsSource}" SelectedIndex="{TemplateBinding SelectedIndex}" SelectedValue="{TemplateBinding SelectedValue}" SelectedValuePath="{TemplateBinding SelectedValuePath}" DisplayMemberPath="{TemplateBinding DisplayMemberPath}" VerticalAlignment="Center" Margin="20,0,10,0" Grid.Row="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

LabeledComboBox.cs

[TemplatePart(Name = "PART_ComboBox", Type = typeof(ComboBox))]
public sealed class LabeledComboBox : Control, IParameterReturnable
{
    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(LabeledComboBox), new PropertyMetadata(""));
    public string Label
    {
        get { return GetValue(LabelProperty).ToString(); }
        set { SetValue(LabelProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(LabeledComboBox), new PropertyMetadata(null));
    public object ItemsSource
    {
        get { return GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(LabeledComboBox), new PropertyMetadata(default(int)));
    public int SelectedIndex
    {
        get { return (int) GetValue(SelectedIndexProperty); }
        set { SetValue(SelectedIndexProperty, value); }
    }

    public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(LabeledComboBox), new PropertyMetadata(null));
    public object SelectedValue
    {
        get { return GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

    public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(LabeledComboBox), new PropertyMetadata(default(string)));
    public string SelectedValuePath
    {
        get { return GetValue(SelectedValuePathProperty).ToString(); }
        set { SetValue(SelectedValuePathProperty, value); }
    }

    public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(LabeledComboBox), new PropertyMetadata(default(string)));
    public string DisplayMemberPath
    {
        get { return GetValue(DisplayMemberPathProperty).ToString(); }
        set { SetValue(DisplayMemberPathProperty, value); }
    }

    private ComboBox _comboBox;

    public LabeledComboBox()
    {
        this.DefaultStyleKey = typeof(LabeledComboBox);
    }

    public LabeledComboBox(List<Parameter> parameterList)
    {
        this.Label = parameterList[0].DisplayName ?? "";
        this.ItemsSource = parameterList;
        this.SelectedValuePath = "DefaultValue";
        this.DisplayMemberPath = "DefaultValue";
        this.SelectedIndex = 0;
        this.DefaultStyleKey = typeof(LabeledComboBox);
    }

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        _comboBox = GetTemplateChild("PART_ComboBox") as ComboBox;

        if (_comboBox != null)
        {
            _comboBox.SelectionChanged += OnComboBoxSelectionChanged;
            if (_comboBox.Items != null)
            {
                this.SelectedIndex = 0;
                _comboBox.SelectedValue = _comboBox.Items[this.SelectedIndex];
            }
        }
    }

    private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.SelectedValue = _comboBox.SelectedValue;
    }

    public string GetKey()
    {
        return Label;
    }

    public string GetValue()
    {
        return SelectedValue.ToString();
    }

}

它将以两种不同的方式调用方式:

It will be called in two different ways:

动态使用C#

stackPanel.Add(new LabeledComboBox(parameterList));

以Xaml表示的静态

<templates:LabeledComboBox Label="Kategorien:" ItemsSource="{Binding ElementName=pageRoot, Path=FeedCategories}" DisplayMemberPath="Name" SelectedValuePath="Name" />

就像我之前说的,我遇到两个问题:

As I said before I got two issues with it:

  1. 如何绑定SelectionChangedEvent以在Xaml中访问它|| C#

  2. 如您所见,我尝试预选第一个项目,该项目不起作用,我也不知道该怎么做

非常感谢您事先提供的所有有益和善意的答案!

Thank you very much for all helpful and well meant answers in advance!

推荐答案

建议不要使用 Header HeaderTemplate 来创建自定义控件并重新创建所有必需的依赖项属性。内置 ComboBox 的属性,将显示该属性,就像在选择菜单上方的 LabeledComboBox 中一样。此外, SelectionChanged 事件将可用。

Instead of creating a custom control and recreating all needed dependency properties, I would suggest you use the Header and HeaderTemplate properties of the built in ComboBox, which will be displayed, just like in your LabeledComboBox, above the selection menu. Additionally the SelectionChanged event will be available.

因此XAML中的用法如下所示:

So the usage in XAML would look like the following:

    <ComboBox
        DisplayMemberPath="Name"
        Header="Kategorien:"
        ItemsSource="{Binding ElementName=pageRoot, Path=FeedCategories}"
        SelectedValuePath="Name"
        SelectionChanged="OnSelectionChanged">
        <ComboBox.HeaderTemplate>
            <DataTemplate>
                <TextBlock
                    Margin="10,0"
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Text="{Binding}" />
            </DataTemplate>
        </ComboBox.HeaderTemplate>
    </ComboBox>

但是如果您不想使用上述方法,则在您的选择中显示更改的事件 LabeledComboBox ,添加以下代码:

But if you don't want to use the above method, to expose the selection changed event in your LabeledComboBox, add the following code:

    private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.SelectedValue = _comboBox.SelectedValue;
        this.RaiseSelectionChanged(e);
    }

    public event EventHandler<SelectionChangedEventArgs> SelectionChanged;

    private void RaiseSelectionChanged(SelectionChangedEventArgs args)
    {
        if (SelectionChanged != null)
        {
            SelectionChanged(this, args);
        }
    }

然后您可以使用创建的 SelectionChanged 事件。

Then you can use the created SelectionChanged event from XAML.

这篇关于Windows 10 Universal App中标记为ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆