簡體   English   中英

如何添加默認的組合框“-選擇用戶-”

[英]How do I add a default comboboxitem “--Select user--”

我正在使用MVVM模式開發WPF應用程序,我有一個組合框,其itemsource從viewmodel綁定,我想在組合框中有一個Default選項,例如“ --Select user--”,這是最好的方法。 這是我的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>
<ComboBox>
SelectedIndex="0"
<ComboBox.ItemsSource>
    <CompositeCollection>
        <ListBoxItem>Please Select</ListBoxItem>
        <CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
    </CompositeCollection>
</ComboBox.ItemsSource>

最簡單的方法是在綁定列表/集合中包含此類項目。 在視圖模型中完成列表的填充后,只需執行

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

它是一個偽代碼,請進行調整。

嗯,另一種方法可以是使用觸發器,讓組合框項目保持原樣

<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 --" />

如果使用的是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(...));

然后,您可以使用在其上設置x:Key指令的DataTemplateComboBox以不同方式顯示它們:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM