簡體   English   中英

根據選擇的不同ComboBoxItem值自動選擇ComboBoxItem

[英]Select ComboBoxItem automatically based on different selected ComboBoxItem value

我有一個帶有一些字段和兩個組合框的表格。 我的視圖模型包含User類的一些實例,其中包含nameprofile屬性。

視圖模型

    public class UserViewModel: INotifyPropertyChanged
    {
        private ObservableCollection<User> usersList = new ObservableCollection<User>();
        private User selectedUser = new User();
        private User newUser = new User();

        public UserViewModel()
        {
        }

        public ObservableCollection<User> UsersList
        {
            get
            {
                return this.usersList;
            }

            set
            {
                this.usersList = value;
                this.OnPropertyChanged();
            }
        }

        public User SelectedUser
        {
            get
            {
                return this.selectedUser;
            }

            set
            {
                this.selectedUser = value;
                this.OnPropertyChanged();
            }
        }

        public User NewUser
        {
            get
            {
                return this.newUser;
            }

            set
            {
                this.newUser = value;
                this.OnPropertyChanged();
            }
        }

        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
 }

代碼隱藏

this.userViewModel = new UserViewModel();
this.DataContext = this.userViewModel;

XAML

<-- The DataContext for this ComboBox is the entire UserViewModel -->
<ComboBox
        Height="Auto"
        Width="Auto"
        IsEditable="True"
        IsTextSearchCaseSensitive="False"
        SelectedItem="SelectedUser">
    <ComboBoxItem Content="Mary" IsSelected="True"></ComboBoxItem>
    <ComboBoxItem Content="John"></ComboBoxItem>
</ComboBox>

如果我從第一個組合框中選擇一個用戶,則所有表單字段都將填充現有數據。 無論我選擇哪個用戶,都將通過SelectedItem屬性將其保存在SelectedUser

<Grid>
    <Grid.DataContext>
        <PriorityBinding>
            <Binding Path="SelectedUser" Converter="{StaticResource NullToDependencyPropertyUnsetConverter}" />
            <Binding Path="NewUser" />
        </PriorityBinding>
    </Grid.DataContext>
    [...]
    <-- The DataContext for this ComboBox will be SelectedUser or NewUser, depending on the case -->
    <ComboBox
            Name="profileComboBox"
            Height="Auto"
            Width="Auto"
            IsEditable="True"
            IsTextSearchCaseSensitive="False"
            SelectedItem="Profile">
        <ComboBoxItem Content="User" IsSelected="True"></ComboBoxItem>
        <ComboBoxItem Content="Admin"></ComboBoxItem>
    </ComboBox>
    [...]
</Grid>

ComboBox會將SelectedUserNewUser作為DataContext,具體取決於SelectedUser是否為null。 然后,無論我選擇什么配置文件,它都將通過SelectedItem屬性保存在先前選擇的用戶的Profile屬性中。

我的問題是,第二個組合框應自動選擇剛在第一個組合框中選擇的用戶的相應配置文件(“用戶”或“管理員”)。 僅使用XAML可以實現嗎?

將第二個ComboBoxSelectedItem綁定到當前所選UserProfile屬性:

SelectedItem="{Binding SelectedUser.Profile}"

要么

SelectedItem="{Binding SelectedItem.Profile, ElementName=userComboBox}"

還從ComboBox刪除項目,並將DisplayMemberPath屬性設置為Profile類型的屬性的名稱:

<ComboBox
        Name = "profileComboBox"
        IsEditable="True"
        IsTextSearchCaseSensitive="False"
        DisplayMemberPath="Name"
        SelectedItem="{Binding SelectedUser.Profile}">
</ComboBox>

暫無
暫無

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

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