簡體   English   中英

帶有“刷新”條目的組合框

[英]ComboBox with “Refresh” Entry

我在項目中遇到以下情況,我想知道實現目標的最佳方法是什么。

目標:擁有一個帶有itemsource綁定的組合框和一個功能類似於刷新按鈕的條目(從數據庫中獲取項目並更新組合框項目)。

目前,我使用itemsource綁定設置了組合框(請參見下文),但是目前我正在為刷新命令而苦苦掙扎。

ItemsSource綁定:

<UserControl.Resources>
<CollectionViewSource x:Key="ProjectSource" Source="{Binding Projects, ElementName=Ancestor}"/>
    <CompositeCollection x:Key="ProjectCollection">
        <CollectionContainer Collection="{Binding Source={StaticResource ProjectSource}}"/>
        <Button Content="Refresh!"/>
    </CompositeCollection>

</UserControl.Resources>

如果Projects是具有項目枚舉的依賴項屬性,則還可以使用帶有refresh命令(ICommand)的另一個依賴項屬性。

我的ComboBox ist定義如下:

<ComboBox SelectedValue="{Binding Project}"
                  ItemsSource="{StaticResource ProjectCollection}"
                  VerticalContentAlignment="Center"
                  HorizontalAlignment="Left"
                  Name="Box"
                  IsHitTestVisible="{Binding IsEditable}"
                  IsEnabled="{Binding IsEnabled, Mode=OneWay, IsAsync=True}">

            <ComboBox.Resources>
                <DataTemplate DataType="{x:Type viewModels:ProjectViewModel}">
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>>
            </ComboBox.Resources>

        </ComboBox>

問題是Command找不到綁定的來源,所以問題是我是在做正確的方法並且找到解決方案,還是我在做錯了方法(這會更好嗎?)。

當然,我可以在組合框旁邊添加一個按鈕,但我想在組合框中添加按鈕。 :)

順便說一句:我正在嘗試遵循MVVM模式。

我過去通過使用后面的代碼解決了這個問題。 加載組合框后,創建一個新的Projects List<objects>並將Refresh string (也許是"<Refresh...>" )添加到列表中,最后將ItemsSource設置到該列表中。 使用模板選擇器顯示適當的DataTemplate。 選擇更改后,請檢查是否選擇了“ Refresh string ,如果是,請刷新並重新加載組合框。 刷新時,可以嘗試將選擇設置回先前選擇的項目或索引0,這樣用戶永遠不會在組合框中選擇“刷新”。

一些片段來演示。

在ctor

SelectedProjectComboBoxTemplateSelector.StringTemplate = FindResource("StringTemplate") as DataTemplate;
SelectedProjectComboBoxTemplateSelector.ProjectTemplate = FindResource("ProjectTemplate") as DataTemplate;
SelectedProjectComboBox.SelectionChanged += SelectedProjectComboBox_SelectionChanged;
SelectedProjectComboBox.ItemTemplateSelector = new SelectedProjectComboBoxTemplateSelector();

void SelectedProjectComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    if (SelectedProjectComboBox.SelectedItem is string && ((string)SelectedProjectComboBox.SelectedItem) == RefreshProjectSelectionItem) {
        object current = e.RemovedItems.Count > 0 ? e.RemovedItems[0] : null;

        bool ret = RefreshData(); // from db
        if (ret) {
            LoadData(); // repopulate combobox
        } else {
            SelectedProjectComboBox.SelectedItem = current;
        }               
    }
}

public class SelectedProjectComboBoxTemplateSelector : DataTemplateSelector {
    public static DataTemplate StringTemplate { get; set; }
    public static DataTemplate ProjectTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container) {
        if (item == null || Designer.IsInDesignMode) return null;

        if (item is string) return StringTemplate;
        if (item is Project) return ProjectTemplate;

        return null;
    }
}

您有主意...如果此解決方案滿足您的需求,這應該足以使您繼續前進。

暫無
暫無

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

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