Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有其他方法可以使用c#(而不是SelectionChangedEventHandler)创建组合框_C#_.net_Silverlight_Combobox_Silverlight 5.0 - Fatal编程技术网

是否有其他方法可以使用c#(而不是SelectionChangedEventHandler)创建组合框

是否有其他方法可以使用c#(而不是SelectionChangedEventHandler)创建组合框,c#,.net,silverlight,combobox,silverlight-5.0,C#,.net,Silverlight,Combobox,Silverlight 5.0,我在c#工作,使用silverlight5。我遇到了一个问题,我必须使用c#(而不是xaml)创建组合框,因为我是动态创建的 我已经使用了selectionchangedventhandler来完成此操作,但我想用其他方式替换它,但不知道是哪种方式 目前我有以下工作代码: ComboBox cb = new ComboBox(); if (param.Type == "ComboBox") // I am reading xml if there is ComboBox in its nod

我在c#工作,使用silverlight5。我遇到了一个问题,我必须使用c#(而不是xaml)创建组合框,因为我是动态创建的

我已经使用了
selectionchangedventhandler
来完成此操作,但我想用其他方式替换它,但不知道是哪种方式

目前我有以下工作代码:

 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;
 }
有没有人可以写信给我,告诉我用c#only(而不是Xaml)实现同样的功能,它必须支持silverlight5? 这将是一个很大的帮助。谢谢

为什么只有c?


实际上,我将动态地使用xml,我不知道它的结构,我将对它进行反序列化,我将使用它获得的对象访问xml节点值。如果我遇到任何具有
ComboBox
的节点,那么我将创建我上面创建的组合框(使用选择更改事件)我必须实现同样的目标,但不使用xaml和Selection changed事件。

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

<ListBox ItemSource="{Binding DynamicItems}">
     <ListBox.ItemTemplate>
        <DataTemplate>
            <ComboBox Text="{Binding Name}"/>
        </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>
然后将自动创建组合框。我更喜欢这种方式,因为在代码隐藏中创建UI元素看起来很糟糕


我知道这不是您问题的解决方案,您只需调整它以适应您的问题。

您可以通过编程方式设置绑定。。。 …但是,我再次强烈建议您阅读
数据模板
,您不会对“仅使用c代码”的方法感到满意

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"
      });
}

谢谢,但我知道怎么做。甚至我也知道如何使用selectionevent。但是我不想这样做。你知道使用c#only的其他方法吗?为什么要使用c#only?那你的代码怎么了?你所说的等价物是什么意思?更准确地说你的意思是什么。也许可以尝试使用Binding.SetBinding使绑定仅从c#开始工作。请告诉我您是否理解我的意图?我不知道您的代码是否正常。您创建了组合框,但此组合框没有任何项。然后设置其选定的索引。你忘记添加项目了吗?如果是,那么对象参数的结构是什么?你知不知道?正如您所写的,您不知道整个xml的结构,不知道单个参数,对吗?是的,我的代码还可以。我已经在组合框中添加了这些项,但我只想给出有用的代码。Martin的解决方案非常接近我想要实现的目标,但有一个例外:灾难性失败(异常de HRESULT:0x8000FFFF(E_意外))在线“新绑定”(“SelectedValue”){Source=cb,StringFormat=“{0}百万”};“我必须[…]只使用c#(不是xaml),因为我动态地执行它“这不是一个真正有效的理由,您可以声明xaml来处理动态内容。定义一个
DataTemplate
,当您的数据(已解析的xml)包含组合节点时使用该模板。灾难性故障(异常de HRESULT:0x8000FFFF(E_意外))在“新绑定(“SelectedValue”){Source=cb,StringFormat=“{0}百万”};“sllauncher.exe”(Silverlight):加载的“c:\Program Files(x86)\Microsoft Silverlight\5.1.30214.0\fr\mscorlib.debug.resources.dll“在VStudio的输出窗口上的System.Windows.dll中发生了类型为“System.exception”的第一次意外异常
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"
      });
}