WPF ComboBox IsEnabled来自Items Count

时间:2018-04-23 13:37:58

标签: wpf xaml combobox isenabled

我想让ComboBox的XAML根据它是否有Items来处理它自己的IsEnabled属性。如果后面的代码中的DataTable返回Items,则id类似于要启用的ComboBox,否则如果没有添加Items,它将保留或成为禁用的控件。这是可能吗?

我目前的ComboBox设置:

int count = Integer.parseInt(request.getParameter("count")); 

if(count >0){ 

for(int i=1;i<=count;i++){ 


 //employee_id_numeric=Integer.parseInt(request.getParameter("employee_id")+i);
 String employee_id = request.getParameter("txt_Employee_id"+i);
String txt_project_name = request.getParameter("txt_project_name"+i); 
String get_header = request.getParameter("txt_Header"+i); 
String get_department = request.getParameter("txt_Department"+i); 
String get_description = request.getParameter("txt_description"+i); 

ps.setString(1, employee_id);   
ps.setString(2, txt_project_name); 
ps.setString(3, get_header); 
ps.setString(4, get_department); 
ps.setString(5, get_description); 

ps.addBatch(); 
} 
} 
ps.executeBatch(); 

1 个答案:

答案 0 :(得分:3)

如果您希望ComboBox在有项目时启用,并在没有项目时禁用,则可以将IsEnabled绑定到HasItems属性:

<ComboBox x:Name="ImportDate" 
    DisplayMemberPath="FileDate" 
    SelectedValuePath="ID" 
    ItemsSource="{Binding Mode=OneWay}" 
    SelectedIndex="0" 
    Style="{DynamicResource sanComboBox_Standard}"
    IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}" />