2015-04-30 26 views
1

我正在開發一個WPF應用程序使用MVVM模式,我有一個組合框與itemssource綁定viewmodel,我想有一個默認選項中的組合框像「 - 選擇用戶 - 「,這樣做的最佳方法是什麼? 這是我的XAML代碼:如何添加默認組合框「 - 選擇用戶 - 」

<ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="5.2,8.2,0,7.8" Grid.Row="5" Width="340" ItemsSource="{Binding Path=Users}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedUser}" Grid.ColumnSpan="2"> 
      <ComboBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock> 
         <TextBlock.Text> 
          <MultiBinding StringFormat="{}{0} {1}"> 
           <Binding Path="FirstName"/> 
           <Binding Path="LastName"/> 
          </MultiBinding> 
         </TextBlock.Text> 
        </TextBlock> 
       </DataTemplate> 
      </ComboBox.ItemTemplate> 
</ComboBox> 

回答

0

那麼另一種方法可以有一個觸發器,讓comboboxitems因爲他們

<Grid> 
    <ComboBox x:Name="mycombo" ItemsSource="{Binding}"/> 
    <TextBlock Text="-- Select User --" IsHitTestVisible="False" Visibility="Hidden"> 
      <TextBlock.Style> 
       <Style TargetType="TextBlock"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=mycombo,Path=SelectedItem}" Value="{x:Null}"> 
            <Setter Property="Visibility" Value="Visible"/> 
          </DataTrigger> 
         </Style.Triggers> 
       </Style> 
      </TextBlock.Style> 
    </TextBlock> 
</Grid> 

或者,如果你能負擔得起改變你的組合框的外觀看起來像一個可編輯的組合框,你可以做到這一點 - -

<ComboBox IsEditable="True" IsReadOnly="True" Text="-- Select User --" /> 
+0

你是什麼意思讓讓comboboxitem,因爲他們 –

+0

沒有在你的組合框項目列表中的其他項目,只是默認的文本。 – Muds

+0

我很困惑我不明白你是什麼意思 –

0
<ComboBox> 
SelectedIndex="0" 
<ComboBox.ItemsSource> 
    <CompositeCollection> 
     <ListBoxItem>Please Select</ListBoxItem> 
     <CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" /> 
    </CompositeCollection> 
</ComboBox.ItemsSource> 
0

最簡單的方法就是你的綁定列表/集合中有這樣的項目。在您的視圖模型中完成填充列表後,只需執行

myViewModel.Users.Insert(0, "--Select user--"); 

它是一個僞代碼,所以請進行調整。

+0

假設用戶是字符串的集合? – Muds

+0

這就是爲什麼我寫了它是一個僞代碼,因爲我不知道「用戶」類 –

+0

yea的規範,所以如果用戶是一個對象,你建議添加一個新的對象? – Muds

0

如果你使用MVVM,那麼你的選擇用戶項目應該是一個集合在您的視圖模型具有所有其他項目的一起。有幾種實現方法,具體取決於數據項的類型。

如果你只是使用普通string s,則很容易...只需添加一個字符串轉換成在第一索引綁定收集數據:

DataBoundCollection.Insert(0, "--Select user--"); 

如果你的數據類型是一個自定義類,那麼你可以使用用於顯示項目的名稱/標識符string屬性來保存該值:

DataBoundCollection.Insert(0, new YourDataType("--Select user--", null, null, 0, 0)); 

另外一個選擇是讓DataBoundCollection基本類型的集合。正常的項目應該派生這個基本類型,因此可以添加到這個集合中。默認的項目可能是另一個派生類型(因此它可以顯示不同),其也可以加入到同一個集合:

// You could even hard code this text into the DefaultType object alternatively 
DataBoundCollection.Add(new DefaultType("--Select user--")); 
DataBoundCollection.Add(new YourDataType(...)); 
DataBoundCollection.Add(new YourDataType(...)); 
... 
DataBoundCollection.Add(new YourDataType(...)); 

然後,您可以以不同的ComboBox使用DataTemplate s表示做沒有顯示出來對他們的x:Key指令集:

<DataTemplate DataType="{x:Type Local:YourDataType}"> 
    <!-- Define how your normal items are displayed here --> 
</DataTemplate> 
<DataTemplate DataType="{x:Type Local:DefaultType}"> 
    <!-- Define how your default item is displayed here --> 
</DataTemplate>