Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# 在另一个应用程序中从UserControl分配comboBox.ItemsSource_C#_.net_Wpf_Xaml_Combobox - Fatal编程技术网

C# 在另一个应用程序中从UserControl分配comboBox.ItemsSource

C# 在另一个应用程序中从UserControl分配comboBox.ItemsSource,c#,.net,wpf,xaml,combobox,C#,.net,Wpf,Xaml,Combobox,我建立了自己的“搜索面板”,里面有一个搜索词的文本框, 用于指定搜索条件的组合框和用于启动搜索的按钮 ucSearchPanel XAML: <UserControl x:Class="TestProjekt.Views.UserControls.ucSearchPanel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

我建立了自己的“搜索面板”,里面有一个搜索词的文本框, 用于指定搜索条件的组合框和用于启动搜索的按钮

ucSearchPanel XAML:

<UserControl x:Class="TestProjekt.Views.UserControls.ucSearchPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    Width="auto" Height="auto">
    <Grid>
        <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal" Grid.Row="0" >
            <Label Name="lblSearch" Width="{Binding ElementName=tbSearch, Path=ActualWidth}" HorizontalContentAlignment="Center">Suche</Label>
            <Label Name="lblSearchCriteria" Width="{Binding ElementName=cbSearchCriteria, Path=ActualWidth}" HorizontalContentAlignment="{Binding ElementName=lblSearch, Path=HorizontalContentAlignment}">Suchkriterium</Label>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Grid.Row="1">
            <TextBox Name="tbSearch" MinWidth="100"></TextBox>
            <ComboBox Name="cbSearchCriteria" MinWidth="{Binding ElementName=tbSearch, Path=MinWidth}"  />
            <Button Name="btnSearch">Suche</Button>
        </StackPanel>
    </Grid>
</UserControl>

苏凯
苏克克里特酒店
苏凯
但是组合框中的搜索标准因用例而异。。。 因此,我希望在不同的应用程序中使用ucSearchPanel中ComboBox的属性ItemsSource

如果我为ucSearchPanel分配标识符x:Name,我可以通过代码隐藏中的C#代码设置ucSearchPanels.NameComboBox.ItemsSource

但是,我也希望在XAML中实现这一点。因此,我创建了一个依赖项属性:

using System.Collections;
using System.Windows;
using System.Windows.Controls;

ucSearchPanel.xaml.cs:

  namespace TestProjekt.Views.UserControls
  {
      /// <summary>
      /// Interaktionslogik für ucSearchRegion.xaml
      /// </summary>
      public partial class ucSearchPanel : UserControl
      {
          public static readonly DependencyProperty cbSearchCriteriaItemsSourceProperty;
          public IEnumerable cbSearchCriteriaItemsSource
          {
              get { return (IEnumerable)GetValue(cbSearchCriteriaItemsSourceProperty); }
              set { SetValue(cbSearchCriteriaItemsSourceProperty, value) }

          }

          public string cbDisplayMemberPath
          {
              get
              {
                  return cbSearchCriteria.DisplayMemberPath;
              }
              set
              {
                  cbSearchCriteria.DisplayMemberPath = value;
              }
          }

          static ucSearchPanel()
          {
              cbSearchCriteriaItemsSourceProperty = DependencyProperty.Register("cbSearchCriteriaItemsSource",
              typeof(IEnumerable), typeof(ucSearchPanel));
          }

          public ucSearchPanel()
          {
              InitializeComponent();
          }
      }
  }
使用系统集合;
使用System.Windows;
使用System.Windows.Controls;
ucSearchPanel.xaml.cs:
命名空间TestProjekt.Views.UserControls
{
/// 
///Interaktionslogik für ucSearchRegion.xaml
/// 
公共部分类ucSearchPanel:UserControl
{
公共静态只读从属属性cbSearchCriteriaItemsSourceProperty;
公共IEnumerable cbSearchCriteriaItemsSource
{
获取{return(IEnumerable)GetValue(cbSearchCriteriaItemsSourceProperty);}
set{SetValue(cbSearchCriteriaItemsSourceProperty,value)}
}
公共字符串cbDisplayMemberPath
{
收到
{
返回cbSearchCriteria.DisplayMemberPath;
}
设置
{
cbSearchCriteria.DisplayMemberPath=值;
}
}
静态搜索面板()
{
cbSearchCriteriaItemsSourceProperty=DependencyProperty.Register(“cbSearchCriteriaItemsSource”,
typeof(IEnumerable)、typeof(ucSearchPanel));
}
公共搜索面板()
{
初始化组件();
}
}
}
正如您在源代码中看到的,我没有直接将依赖项属性分配给ComboBox.ItemsSource。 因此,如果将ObservableCollection绑定到属性,则ucSearchPanel中的组合框中不会显示任何项目

我想知道如何以及在哪里在C代码中指定公共静态只读DependencyProperty cbSearchCriteriaItemsSourceProperty引用cbSearchCriteria.ItemsSource

我想提前感谢你的帮助,并留下来


非常感谢

我已通过在构造函数中添加以下代码行修复了我的问题:

  Binding binding = new Binding("cbSearchPlaceItemsSource");
  binding.Source = this;
  cbSearchPlace.SetBinding(ComboBox.ItemsSourceProperty, binding);

现在它可以按我的要求工作。

您可以在
DependencyProperty.Register()中指定一个委托。
Hello Henk,谢谢您的回答。但我不知道你是什么意思。你能给我举个例子吗?