在绑定时设置ComboBox.SelectedItem吗?

时间:2019-03-11 00:54:53

标签: wpf

我在DataTemplate中有一个ComboBox,它由DataGrid中的单元格模板选择器选择。

当ComboBox绑定到ItemsItem时,如何将SelectedItem设置为零?通常只有一个项目,我希望它可以立即显示,而不必由用户选择。

我的DataGrid列如下所示:

<DataGridTemplateColumn Header="Qty Avl">
  <DataGridTemplateColumn.CellTemplateSelector>
    <selectors:PartAvailableSelector StrTemplate="{StaticResource PartAvailableAtStrTemplate}">
      <selectors:PartAvailableSelector.NetTemplate>
        <DataTemplate>
          <ComboBox ItemsSource="{Binding AltLocations}"
                    DisplayMemberPath="Name"
                    SelectedItem="0"
                    />
        </DataTemplate>
      </selectors:PartAvailableSelector.NetTemplate>
    </selectors:PartAvailableSelector>
  </DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>

我的选择器具有DataTemplate属性,只是因为它更容易。我内联了这篇文章的NetTemplate模板。我通常在窗口资源中保存它。

1 个答案:

答案 0 :(得分:1)

COLOR 15 DO PRINT "Enter values(y/n)";: INPUT x$ IF LCASE$(x$) = "n" THEN END PRINT "Enter X,Y,Z";: INPUT X, Y, Z GOSUB Calculate LOOP END Calculate: SELECT CASE X CASE IS > 0 SELECT CASE Y CASE IS > 0 SELECT CASE Z CASE IS > 0 PRINT "X is positive, Y is positive, Z is positive." CASE IS < 0 PRINT "X is positive, Y is positive, Z is negative." CASE ELSE PRINT "X is positive, Y is positive, Z is zero." END SELECT CASE IS < 0 SELECT CASE Z CASE IS > 0 PRINT "X is positive, Y is negative, Z is positive." CASE IS < 0 PRINT "X is positive, Y is negative, Z is negative." CASE ELSE PRINT "X is positive, Y is negative, Z is zero." END SELECT CASE ELSE SELECT CASE Z CASE IS > 0 PRINT "X is positive, Y is zero, Z is positive." CASE IS < 0 PRINT "X is positive, Y is zero, Z is negative." CASE ELSE PRINT "X is positive, Y is zero, Z is zero." END SELECT END SELECT CASE IS < 0 SELECT CASE Y CASE IS > 0 SELECT CASE Z CASE IS > 0 PRINT "X is negative, Y is positive, Z is positive." CASE IS < 0 PRINT "X is negative, Y is positive, Z is negative." CASE ELSE PRINT "X is negative, Y is positive, Z is zero." END SELECT CASE IS < 0 SELECT CASE Z CASE IS > 0 PRINT "X is negative, Y is negative, Z is positive." CASE IS < 0 PRINT "X is negative, Y is negative, Z is negative." CASE ELSE PRINT "X is negative, Y is negative, Z is zero." END SELECT CASE ELSE SELECT CASE Z CASE IS > 0 PRINT "X is negative, Y is zero, Z is positive." CASE IS < 0 PRINT "X is negative, Y is zero, Z is negative." CASE ELSE PRINT "X is negative, Y is zero, Z is zero." END SELECT END SELECT CASE ELSE SELECT CASE Y CASE IS > 0 SELECT CASE Z CASE IS > 0 PRINT "X is zero, Y is positive, Z is positive." CASE IS < 0 PRINT "X is zero, Y is positive, Z is negative." CASE ELSE PRINT "X is zero, Y is positive, Z is zero." END SELECT CASE IS < 0 SELECT CASE Z CASE IS > 0 PRINT "X is zero, Y is negative, Z is positive." CASE IS < 0 PRINT "X is zero, Y is negative, Z is negative." CASE ELSE PRINT "X is zero, Y is negative, Z is zero." END SELECT CASE ELSE SELECT CASE Z CASE IS > 0 PRINT "X is zero, Y is zero, Z is positive." CASE IS < 0 PRINT "X is zero, Y is zero, Z is negative." CASE ELSE PRINT "X is zero, Y is zero, Z is zero." END SELECT END SELECT END SELECT RETURN 将保留SelectedItem中的整个对象,以将0项设置为选中状态,您需要设置ItemsSource或在ViewModel中将SelectedIndex="0"绑定到{ {1}}

SelectedItem="{Binding SLocation}"

AltLocations[0]

Vm

 <ComboBox ItemsSource="{Binding AltLocations}"
                DisplayMemberPath="Name"
                SelectedIndex="0"
                />

// Ctor

<ComboBox ItemsSource="{Binding AltLocations}" DisplayMemberPath="Name" SelectedItem="{Binding SLocation}" />