分配组合框.ItemsSource来自另一个应用程序中的UserControl

本文关键字:应用程序 UserControl 另一个 组合 ItemsSource 分配 | 更新日期: 2023-09-27 18:14:06

我建立了自己的"搜索面板",其中有一个文本框用于搜索词,一个用于指定搜索条件的组合框和一个用于开始搜索的按钮。

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,我可以通过code - behind中的c#代码设置ucSearchPanel . 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();
          }
      }
  }

正如您在源代码中看到的,我没有将Dependency属性分配给ComboBox。直接ItemsSource。因此,如果我将ObservableCollection绑定到属性,则在ucSearchPanel中的ComboBox中不会显示任何项。

我想知道我在c#代码中如何以及在哪里指定公共静态只读DependencyProperty cbSearchCriteriaItemsSourceProperty引用cbsearchcriterion . itemssource .

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

with best regards

分配组合框.ItemsSource来自另一个应用程序中的UserControl

通过在构造函数中添加以下代码行,我已经解决了这个问题:

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