簡體   English   中英

在datagrid上導航時刷新組合框

[英]Refresh combobox when navigate on datagrid

請幫忙!

我在互聯網上進行了許多研究,但沒有找到解決我問題的方法。

我有一份食物表格。 表單上有一個網格,通過它我可以在食物桌上導航。 屏幕上(不在網格中)有一個包含類別的組合框。 組合框填充了類別表中的類別。 當我更改數據網格上的記錄時,除了組合框外,表單上的每個字段都更新了。

所以我的問題是:當我在網格上導航時,我該怎么做才能刷新組合框,以顯示保存的類別? 在類別表中,類別具有“ id”字段,在食物表中,具有“ categoryid”字段。

我在xaml文件中有此文件:

<ComboBox x:Name="categoryComboBox" Grid.Row="5" Grid.Column="1" Margin="3,4,20,0" Grid.ColumnSpan="3"
                  ItemsSource="{Binding Source={StaticResource categoryViewSource}}"
                  SelectedValuePath="CategoryId"
                  DisplayMemberPath="CatName"
                  SelectedItem="{Binding CategoryId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  Height="25" VerticalAlignment="Top">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>

如我所見,您的代碼中有一個小錯誤。 我應該使用SelectedValue而不是SelectedItem。 因此,對其進行更改,我認為它將正常運行。 另外,您不需要像我之前建議的那樣進行縮放。 這是示例:

XAML

     <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
        <ComboBox x:Name="categoryComboBox" ItemsSource="{Binding Source={StaticResource categoryViewSource}}"
              SelectedValuePath="CategoryId"
              DisplayMemberPath="CatName"
              SelectedValue="{Binding CategoryId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              Height="25" VerticalAlignment="Top">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>
        <Button Content="Change Category" Command="{Binding SelectionChangedCommand}"></Button>
    </StackPanel>

數據上下文

public class MyComboDataContext:BaseObservableObject
{
    private int _categoryId;
    private ICommand _selectionChangedCommand;

    public MyComboDataContext()
    {
        CategoryId = 1;
    }

    public int CategoryId
    {
        get { return _categoryId; }
        set
        {
            _categoryId = value;
            OnPropertyChanged();
        }
    }


    public ICommand SelectionChangedCommand
    {
        get { return _selectionChangedCommand ?? (_selectionChangedCommand = new RelayCommand(SelectionChanger)); }
    }

    private void SelectionChanger()
    {
        CategoryId += 1;
        if (CategoryId == 4)
            CategoryId = 1;
    }
}

說明:

首先,這是一個模擬組合更新的示例。 在此,組合框選擇的值在每次單擊按鈕時都會更改。 在您的示例中,類別選擇應影響組合選擇的值。 因此,每次發生網格類別選擇時,都應將所選類別ID推入組合的SelectedValue綁定到該屬性中。

為了幫助您,請使用以下內容更新您的問題:

  1. 您的輸出窗口中是否存在任何綁定表達式錯誤?
  2. 您如何在代碼中處理DataGrid選擇。
  3. 數據網格選擇如何影響組合框SelectedValue(必須為選定值)?

讓我知道您是否需要更多幫助。 如果我的回答有幫助,請隨時將您的問題標記為已回答。

暫無
暫無

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

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