【问题标题】:Is there any other way to create combo box using c# (rather than SelectionChangedEventHandler)有没有其他方法可以使用 c#(而不是 SelectionChangedEventHandler)创建组合框
【发布时间】:2014-05-16 14:38:25
【问题描述】:

我正在使用 c# 并使用 silverlight5。我遇到了一个问题,我必须只使用 c#(不是 xaml)来创建组合框,因为我是动态进行的。

我已经使用SelectionChangedEventHandler 这样做,但我想用其他方式替换它但不知道哪种方式?

目前我有这个工作代码:

 ComboBox cb = new ComboBox();
 if (param.Type == "ComboBox") // I am reading xml if there is ComboBox in its node then i create combo box using c# and this "param" is an object
 {

     TextBlock txtblk2 = new TextBlock(); //This textBlock is to print the selected value from Combo Box

     cb.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
     cb.SelectedIndex = cb.Items.Count - 1;
     txtblk2.Text = cb.SelectedValue.ToString() + " millions";

 }
 void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
 {
     MessageBox.Show("comboBox1_SelectionChanged1");
     txtblk2.Text = cb.SelectedValue.ToString() + " millions";
     txtblk2.FontSize = 14;
 }

有人可以给我写任何等效的方法来实现相同的仅使用必须支持 silverlight5 的 c#(不是 Xaml)吗? 会有很大的帮助。谢谢。

为什么只有 c#?

实际上,我将动态拥有我不知道其结构的 xml,我将对其进行反序列化,如果我遇到任何具有 <Type>ComboBox<Type> 的节点,我将使用它获得的对象来访问 xml 节点值,然后我将创建组合我在上面创建的框(使用选择更改事件)与我必须实现的相同,但不使用 xaml 和选择更改事件。

【问题讨论】:

  • “我必须 [...] 只使用 c#(不是 xaml),因为我是动态执行的”这不是一个真正的正当理由,您可以声明 xaml 来处理动态内容。定义一个 DataTemplate,只要您的数据(解析的 xml)包含 combo-Node.

标签: c# .net silverlight combobox silverlight-5.0


【解决方案1】:

为什么不使用 XAML?您可以将 ListBox 与绑定到对象集合的项绑定,其中 DataTemplate 设置为例如:

<ListBox ItemSource="{Binding DynamicItems}">
     <ListBox.ItemTemplate>
        <DataTemplate>
            <ComboBox Text="{Binding Name}"/>
        </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

在后面的代码中:

     if (param.Type == "ComboBox")
     {
          DynamicItem item = new DynamicItem(){Name="param.Name"};
          DynamicItems.Add(item);
     }

然后 ComboBox 将被自动创建。我更喜欢这种方式,因为在后面的代码中创建 UI 元素看起来很糟糕。

我知道这不能解决您的问题,您必须根据您的问题进行调整。

【讨论】:

  • 谢谢,但我知道这样做。即使我知道使用 selectionevent 改变了。但我不想这样做。您知道仅使用 c# 的任何其他方式吗?
  • 为什么你只想用 c# 来做这个?那你的代码有什么问题?你说的等价是什么意思?更准确地说出你的意思。也许尝试使用 Binding.SetBinding 使该绑定仅在 c# 中工作
  • 如果你明白我想要做什么,请告诉我吗?
  • 不知道你的代码是否正常。您创建组合框,并且此组合框没有项目。并设置其选定的索引。您忘记添加项目了吗?如果是,那么对象参数的结构是什么?你知不知道?正如您所写,您不知道整个 xml 的结构,而不是单个参数,对吗?
  • 是的,我的代码没问题。我已经在组合框中添加了项目,但我只想提供有用的代码。Martin 的解决方案非常接近我想要实现的目标,但有一个例外:Catastrophic failure (Exception de HRESULT : 0x8000FFFF (E_UNEXPECTED)) 在线"new Binding("SelectedValue") { Source = cb, StringFormat = "{0} 百万" }); "
【解决方案2】:

您可以通过编程方式设置绑定... ...但再次,我强烈建议您阅读有关 DataTemplates 的内容,您不会对您的“仅 c#-code”方法感到满意。

if (param.Type == "ComboBox")
{
    ComboBox cb = new ComboBox();
    TextBlock tb = new TextBlock(){FontSize = 14};
    tb.SetBinding(TextBlock.TextProperty,
      new Binding("SelectedValue")
      {
         Source = cb, StringFormat = "{0} millions"
      });
}

【讨论】:

  • 灾难性故障(异常 de HRESULT : 0x8000FFFF (E_UNEXPECTED))在线“new Binding("SelectedValue") { Source = cb, StringFormat = "{0}million" }); "
  • sllauncher.exe' (Silverlight): Loaded 'c:\Program Files (x86)\Microsoft Silverlight\5.1.30214.0\fr\mscorlib.debug.resources.dll' 类型的第一次机会异常VStudio 输出窗口的 System.Windows.dll 中发生“System.Exception”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2022-01-10
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2020-02-22
相关资源
最近更新 更多