自动选择Comboxbox的第一项

时间:2015-06-13 07:19:10

标签: c# wpf

我有一个ComboBox,用于绑定ViewModel的属性。

<div ng-controller="TestModal">
  <button type="button" ng-click="openTestModal()">
    Open Modal
  </button>
  <div ng-cloak="">
    {{ model.kmlUrl }}
  </div>
</div>
<script type="text/ng-template" id="test-modal.html">
  <input type="text" placeholder="url" ng-model="model.kmlUrl">
  <div class="modal-footer">
    <button type="button" ng-click="closeTestModal()">Close</button>
  </div>
</script>

(function(){
  angular.module('test', ['mgcrea.ngStrap']);

  angular.module('test').controller('TestModal', function($scope, $modal){
    var modal = $modal({
      scope: $scope,
      title: 'Test Modal',
      contentTemplate: 'test-modal.html',
      show: false
     });

    $scope.model = {
      kmlUrl: 'https://www.amazon.com'
    };

    $scope.openTestModal = function(){
     modal.show();
    };

    $scope.closeTestModal = function(){
      modal.hide();
    };

  });
})();

我想在有项目时选择自动的第一项。

我使用<ComboBox ItemsSource="{Binding UserCollection}" SelectedValuePath="Id" DisplayMemberPath="UserName" SelectedValue="{Binding SelectedUser}" /> IsSynchronizedWithCurrentItem="True"但不要选择项目。

2 个答案:

答案 0 :(得分:0)

您可以将绑定属性模式添加为双向

<ComboBox  ItemsSource="{Binding UserCollection}"    DisplayMemberPath="UserName"   SelectedItem="{Binding Path=Id}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0"/>

答案 1 :(得分:0)

绑定SelectedItem而不是SelectedValue

<ComboBox ItemsSource="{Binding UserCollection}"  
          SelectedItem="{Binding SelectedUser}"     
          SelectedValuePath="Id"
          DisplayMemberPath="UserName"  
 />

然后将SelectedUser设置为列表中的第一项(如果尚未设置):

SelectedUser = UserCollection[0];