2011-08-17 77 views
0

我有一个用户控件与组合框。在组合框中绑定SetectedItem WPF

<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();    
    }   
} 

我有使用用户控件的窗口:

<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)的问题, 它不是在所有更新....

回答

0

我想这是因为你的SelectedItemstring类型,而您的收藏是DataTable (其中列举了DataRow)。尝试更改您的集合IEnumerable<string>

+0

我从数据库中获取所有数据,使用DataTable更方便。我首先使用SelectedItem作为DataRow,但它仍然没有工作。还有其他解决方案吗? – MoranSmutko 2011-08-17 11:46:33