代码之家  ›  专栏  ›  技术社区  ›  sindre j

在Silverlight 3组合框中使用项模板时未触发SelectionChanged

  •  1
  • sindre j  · 技术社区  · 16 年前

    我在Silverlight组合框中遇到了一些奇怪的行为。我从一些简单的代码开始:

    XAML:

    <ComboBox Name="drpInstallation" SelectionChanged="drpInstallation_SelectionChanged" />
    

    反恐精英:

    List<string> installations = new List<string>();
    installations.Add("Testing 123");
    installations.Add("Antoher test");
    installations.Add("Yeah");
    drpInstallation.ItemsSource = installations;
    

    单击某个项目时,一切都正常。但是,如果我使用如下组合框中的项模板:

    XAML:

    <ComboBox Name="drpInstallation" SelectionChanged="drpInstallation_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ComboBoxItem Content="{Binding Installation}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    

    反恐精英:

    ICollection<InstallationClass> installations = a list of the installation class;
    drpInstallation.ItemsSource = installations;
    

    安装类.cs:

    public class InstallationClass
    {
        public int PK;
        public string Installation;
    }
    

    现在,组合框正确显示,但是当我单击 文本 如果没有发生任何事情。如果我只单击文本本身的右侧,则该项将像正常一样被选中。重点是;自然的做法是单击文本本身,而不是单击文本的左侧或右侧。知道为什么会发生这种情况,知道如何纠正吗?这是Silverlight错误吗?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Eric Mickelsen    15 年前

    您的数据模板应如下所示:

    <ComboBox Name="drpInstallation" SelectionChanged="drpInstallation_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Installation}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    

    问题是组合框会消耗单击事件,而不是使其冒泡。