【问题标题】:Binding SetectedItem in ComboBox WPF在 ComboBox WPF 中绑定 SetectedItem
【发布时间】:2011-10-28 19:12:46
【问题描述】:

我有一个带有 ComboBox 的 UserControl。

  <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) 有问题, 根本没有更新......

【问题讨论】:

    标签: wpf data-binding mvvm combobox selecteditem


    【解决方案1】:

    我认为这是因为您的 SelectedItem 具有 string 类型,而您的集合是 DataTable(枚举 DataRow)。尝试将您的收藏更改为IEnumerable&lt;string&gt;

    【讨论】:

    • 我从数据库中获取所有数据,使用DataTable更方便。我起初使用 SelectedItem 作为 DataRow 但它仍然没有工作。还有其他解决方案吗?
    • 您应该尝试从视图中抽象出数据库特定的东西。视图可以与通过服务层返回的普通旧 CLR 对象 (POCO) 类进行交互。这通常称为数据传输对象,其主要目标是在服务与其客户端之间传输数据。
    猜你喜欢
    • 2011-11-01
    • 2015-04-07
    • 1970-01-01
    • 2020-10-10
    • 2015-06-28
    • 2010-10-24
    • 2013-04-18
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多