在 ComboBox WPF 中绑定 SetectedItem

发布于 2024-11-30 03:56:10 字数 4009 浏览 1 评论 0原文

我有一个带有组合框的用户控件。

  <ComboBox Grid.Row="8" Grid.Column="1" 
             VerticalAlignment="Center"
              x:Name="cmbCategory"
              ItemsSource="{Binding ElementName=ucAppiGeneralInfo, Path=Categories, Mode=TwoWay}"
              SelectedItem="{Binding ElementName=ucAppiGeneralInfo, Path=SelectedCategory, Mode=TwoWay}"
              IsEditable="True"                            
              IsSynchronizedWithCurrentItem="True"     
              SelectedValuePath="CAT_ID"
              TextSearch.TextPath="CAT_NAME">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=CAT_NAME}"/>
                    <TextBlock Text=" - "/>
                    <TextBlock Text="{Binding Path=PUBLIC_DESCRIPTION}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

背后的代码是:

  public partial class AppiGeneralInfoUC : UserControl
{     

    public DataTable Categories
    {
        get { return (DataTable)GetValue(CategoriesProperty); }
        set { SetValue(CategoriesProperty, value);}
    }

    public static readonly DependencyProperty CategoriesProperty =
                    DependencyProperty.Register(
                    "Categories",
                    typeof(DataTable),
                    typeof(AppiGeneralInfoUC),
                    new UIPropertyMetadata(null));

    public String SelectedCategory
    {
        get { return (String)GetValue(SelectedCategoryProperty); }
        set
        {
            SetValue(SelectedCategoryProperty, value);                
        }
    }

    public static readonly DependencyProperty SelectedCategoryProperty =
        DependencyProperty.Register(
        "SelectedCategory",
        typeof(String), 
        typeof(AppiGeneralInfoUC), 
        new UIPropertyMetadata(null));



    public AppiGeneralInfoUC()
    {
        InitializeComponent();            
    }        
}

我有一个使用 UserControl 的窗口:

 <TabControl>
        <TabItem Header="Information">
            <my:AppiGeneralInfoUC x:Name="ucAppiGeneralInfo" 
               Categories="{Binding Path=Categories, Mode=TwoWay}" 
              SelectedCategory="{Binding Path=SelectedCategory, Mode=TwoWay}" />
        </TabItem>

背后的代码是:

public partial class ApplicationWindow : Window
{
    VMBase appiGeneralInfoWin = new AppiGeneralInfoVM();

    public ApplicationWindow()
    {
        InitializeComponent();
        ucAppiGeneralInfo.DataContext = appiGeneralInfoWin;
    } 

  public void updateAction(string cat_id)
    {
        this.Title = "Update application";
        (appiGeneralInfoWin as AppiGeneralInfoVM).setSelectedCategory(cat_id);
    }  ...

最后我有 ViewModel 类:

  class AppiGeneralInfoVM : VMBase
{
    private DataTable categories = null;
    private String selectedCategory = null;

    public DataTable Categories
    {
        get { return this.categories; }
        set
        {
            this.categories = value;
            this.OnPropertyChanged("Categories");
        }
    }

    public String SelectedCategory
    {
        get { return this.selectedCategory; }
        set
        {                
            this.selectedCategory = value;               
            this.OnPropertyChanged("SelectedCategory");
        }
    }

    public AppiGeneralInfoVM()
    {
        ServicesLoader.LoadRunTimeServices();
        Categories = GetService<CategoryBLL>().getCategories();

    }

    public void setSelectedCategory(string cat_id)
    {
        SelectedCategory = Categories.Select("cat_id =" + "'"+cat_id+"'")[0]["CAT_NAME"].ToString();
    }

一切正常,但我对 selectedItem (SelectedCategory) 有问题, 根本就没有更新啊......

I have a UserControl with ComboBox.

  <ComboBox Grid.Row="8" Grid.Column="1" 
             VerticalAlignment="Center"
              x:Name="cmbCategory"
              ItemsSource="{Binding ElementName=ucAppiGeneralInfo, Path=Categories, Mode=TwoWay}"
              SelectedItem="{Binding ElementName=ucAppiGeneralInfo, Path=SelectedCategory, Mode=TwoWay}"
              IsEditable="True"                            
              IsSynchronizedWithCurrentItem="True"     
              SelectedValuePath="CAT_ID"
              TextSearch.TextPath="CAT_NAME">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=CAT_NAME}"/>
                    <TextBlock Text=" - "/>
                    <TextBlock Text="{Binding Path=PUBLIC_DESCRIPTION}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

The code behind is:

  public partial class AppiGeneralInfoUC : UserControl
{     

    public DataTable Categories
    {
        get { return (DataTable)GetValue(CategoriesProperty); }
        set { SetValue(CategoriesProperty, value);}
    }

    public static readonly DependencyProperty CategoriesProperty =
                    DependencyProperty.Register(
                    "Categories",
                    typeof(DataTable),
                    typeof(AppiGeneralInfoUC),
                    new UIPropertyMetadata(null));

    public String SelectedCategory
    {
        get { return (String)GetValue(SelectedCategoryProperty); }
        set
        {
            SetValue(SelectedCategoryProperty, value);                
        }
    }

    public static readonly DependencyProperty SelectedCategoryProperty =
        DependencyProperty.Register(
        "SelectedCategory",
        typeof(String), 
        typeof(AppiGeneralInfoUC), 
        new UIPropertyMetadata(null));



    public AppiGeneralInfoUC()
    {
        InitializeComponent();            
    }        
}

I have a window which use the UserControl:

 <TabControl>
        <TabItem Header="Information">
            <my:AppiGeneralInfoUC x:Name="ucAppiGeneralInfo" 
               Categories="{Binding Path=Categories, Mode=TwoWay}" 
              SelectedCategory="{Binding Path=SelectedCategory, Mode=TwoWay}" />
        </TabItem>

the code behind is:

public partial class ApplicationWindow : Window
{
    VMBase appiGeneralInfoWin = new AppiGeneralInfoVM();

    public ApplicationWindow()
    {
        InitializeComponent();
        ucAppiGeneralInfo.DataContext = appiGeneralInfoWin;
    } 

  public void updateAction(string cat_id)
    {
        this.Title = "Update application";
        (appiGeneralInfoWin as AppiGeneralInfoVM).setSelectedCategory(cat_id);
    }  ...

And finally I have ViewModel class:

  class AppiGeneralInfoVM : VMBase
{
    private DataTable categories = null;
    private String selectedCategory = null;

    public DataTable Categories
    {
        get { return this.categories; }
        set
        {
            this.categories = value;
            this.OnPropertyChanged("Categories");
        }
    }

    public String SelectedCategory
    {
        get { return this.selectedCategory; }
        set
        {                
            this.selectedCategory = value;               
            this.OnPropertyChanged("SelectedCategory");
        }
    }

    public AppiGeneralInfoVM()
    {
        ServicesLoader.LoadRunTimeServices();
        Categories = GetService<CategoryBLL>().getCategories();

    }

    public void setSelectedCategory(string cat_id)
    {
        SelectedCategory = Categories.Select("cat_id =" + "'"+cat_id+"'")[0]["CAT_NAME"].ToString();
    }

Everything works well but i have problem with the selectedItem (SelectedCategory),
it's not update at all....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜血染红嫁衣 2024-12-07 03:56:10

我认为发生这种情况是因为您的 SelectedItem 具有 string 类型,而您的集合是 DataTable (枚举 DataRow)。尝试将您的集合更改为 IEnumerable

I think it happens because your SelectedItem has string type while your collection is DataTable (which enumerates DataRow). Try changing your collection to be IEnumerable<string>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文