簡體   English   中英

使用caliburn micro和BindableCollection從代碼在組合框中設置selecteditem

[英]Set selecteditem in combobox from code using caliburn micro with BindableCollection

我有一個通過視圖模型中的屬性鏈接的組合框。 組合框的itemsource屬性包含具有string和int值的bindablecollection。 視圖模型中的selecteditem已更改,采用了另一個模型中的值。 我可以設置該屬性,但是組合框不會更新顯示。

我嘗試在selecteditem屬性中設置兩個值。 我試圖使用mdel,string和int來制作不同格式的selecteditem屬性。 我可以使用SelectedIndex獲取要更改的組合框,但似乎重置了下拉列表中的第一個。 我嘗試了Google搜索中的所有不同選項,但似乎都沒有用。 我認為所選項目的語法不正確

a

                    <ComboBox Grid.Row="2" Grid.Column="1"
                              HorizontalAlignment="Left" VerticalAlignment="Center"
                              Height="30" Width="100"
                              Margin="10"
                              x:Name="TourTabs"                                  
                              DisplayMemberPath="TourTab">

                        <!--<ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding TourTab, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>-->
                    </ComboBox>

道具模型

public class TourTabsPropModel
{
    public int TourTab_ID { get; set; }
    public string TourTab { get; set; }
}

視圖模型

    private BindableCollection<TourTabsPropModel> _tourTabs = new BindableCollection<TourTabsPropModel>();
    public BindableCollection<TourTabsPropModel> TourTabs
    {
        get { return _tourTabs; }
        set { _tourTabs = value; }
    }
    private TourTabsPropModel _selectedTourTab;
    public TourTabsPropModel SelectedTourTab
    {
        get
        {
            return _selectedTourTab;
        }
        set
        {
            _selectedTourTab = value;
            NotifyOfPropertyChange(() => SelectedTourTab);
        }
    }
    private TourNamePropModel _tourNameSelectedTour;
    public TourNamePropModel TourNameSelectedTour
    {
        get { return _tourNameSelectedTour; }
        set
        {
            _tourNameSelectedTour = value;

            TourNameTextBox = TourNameSelectedTour.TourName;

            SelectedTourTab = new TourTabsPropModel();
            SelectedTourTab.TourTab_ID = TourNameSelectedTour.TourTabID;
            SelectedTourTab.TourTab = TourNameSelectedTour.TourTab;
        }
    }

當前模型中有8個字段,每個字段都有一個字母和一個對應的數字(我需要輸入int但要顯示字符串)我希望組合框在代碼中更改后顯示正確的字符串值。 目前,返回的selectedItem是正確的,但是組合框未更新顯示的字符串值。 我正在學習這一點,可以得到一個簡單的字符串值來工作,但是我無法得到一個bindablecollection來工作,我知道我缺少一些簡單的東西。

好了,終於解決了。 在xaml中,需要設置SelectedValuePath,然后綁定到SelectedValue,因此僅需要使用一種類型(在我的情況下為整數)。

暫無
暫無

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

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