Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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# 多个组合框的相同内容_C#_.net_Wpf_Vb.net_Xaml - Fatal编程技术网

C# 多个组合框的相同内容

C# 多个组合框的相同内容,c#,.net,wpf,vb.net,xaml,C#,.net,Wpf,Vb.net,Xaml,我想在多个组合框中显示相同的组合框项目 <ComboBox> <ComboBoxItem Content="1" /> <ComboBoxItem Content="2" /> <ComboBoxItem Content="3" /> <ComboBoxItem Content="4" /> <ComboBoxItem Content="5" /> </ComboBox>

我想在多个组合框中显示相同的组合框项目

<ComboBox>
    <ComboBoxItem Content="1" />
    <ComboBoxItem Content="2" />
    <ComboBoxItem Content="3" />
    <ComboBoxItem Content="4" />
    <ComboBoxItem Content="5" />
</ComboBox>


有没有一种简单的方法可以在不重复代码的情况下仅在XAML中执行此操作(不使用代码隐藏)?

简单-将组合框数据绑定到同一数据源


然后,定义公共属性CommonItems,您可以在其中设置要在多个Items控件中使用的列表:

 public List<string> CommonItems {get;set;}
公共列表公共项{get;set;}
在窗口UI代码(xaml文件)中,您可以将CommonItems列表用作多个控件的ItemSource,它将起作用。

var priceList=new list
   var priceList = new List<int>
                        {
                            1,
                            2,
                            3,
                            4,
                            5
                        };

    //Now we can use CopyTo() Method

    priceList.CopyTo(insuredList);


    ComboBox1.Datasource=priceList;
    ComboBox2.Datasource=insuredList;
{ 1. 2. 3. 4. 5. }; //现在我们可以使用CopyTo()方法 价格表。复印件(保险清单); ComboBox1.Datasource=priceList; ComboBox2.Datasource=insuredList;
//没有代码隐藏方法:

您需要为每个ComboBox创建新的ComboBoxItems。通常,您将使用一个源集合,并将其应用于两个组合框,然后它们将自己创建新项


您也可以使用。将您自己的样式(模板)添加到全局资源允许您与多个控件共享。

要回答您的问题“是”,您可以在Xaml中创建一个公共数组,并将其分配给组合框的ItemsSource。它看起来像这样。这可以放在应用程序资源中,以实现程序范围内的可见性

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
   <Window.Resources>
       <x:ArrayExtension x:Key="myArray" Type="system:String">
           <system:String>1</system:String>
           <system:String>2</system:String>
           <system:String>3</system:String>
           <system:String>4</system:String>
           <system:String>5</system:String>
       </x:ArrayExtension>
   </Window.Resources>
   <Grid>
       <ComboBox Height="23" ItemsSource="{StaticResource myArray}" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
       <ComboBox Height="23" ItemsSource="{StaticResource myArray}" HorizontalAlignment="Left" Margin="148,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" />
   </Grid>
</Window>

1.
2.
3.
4.
5.

如果您不知道如何使用数据绑定以及如何处理数据上下文,您应该阅读一些教程,因为我或其他人提供的快速示例对您没有帮助,因为您无法理解它的工作原理。快乐编码!无论是使用代码隐藏还是附加ViewModel中的代码,您都必须在公共位置提供该集合,以便所有组合框都可以访问它。还有一种方法,您可以在资源字典中或直接在使用组合框的window.resources中定义项目列表。
   var priceList = new List<int>
                        {
                            1,
                            2,
                            3,
                            4,
                            5
                        };

    //Now we can use CopyTo() Method

    priceList.CopyTo(insuredList);


    ComboBox1.Datasource=priceList;
    ComboBox2.Datasource=insuredList;
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
   <Window.Resources>
       <x:ArrayExtension x:Key="myArray" Type="system:String">
           <system:String>1</system:String>
           <system:String>2</system:String>
           <system:String>3</system:String>
           <system:String>4</system:String>
           <system:String>5</system:String>
       </x:ArrayExtension>
   </Window.Resources>
   <Grid>
       <ComboBox Height="23" ItemsSource="{StaticResource myArray}" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
       <ComboBox Height="23" ItemsSource="{StaticResource myArray}" HorizontalAlignment="Left" Margin="148,10,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" />
   </Grid>
</Window>