绑定复选框将选定值文本框绑定到与绑定到下拉列表的属性不同的属性(Binding checkbox selected value textbox to different property than p

编程入门 行业动态 更新时间:2024-10-23 21:27:50
绑定复选框将选定值文本框绑定到与绑定到下拉列表的属性不同的属性(Binding checkbox selected value textbox to different property than properties bound to dropdown)

我的组合框绑定了3个属性(IsVisited,Address和City):

<ComboBox Height="100" HorizontalAlignment="Left" Margin="100,25,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="300"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsVisited}" Width="150" /> <TextBlock Text="{Binding Address}" Width="100" /> <TextBlock Text="{Binding City}" Width="100" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

我想创建一个带有下拉列表的wpf组合框,其中包含两列

IsVisited(复选框) 城市(String)

组合框的选定文本框部分应仅显示“地址”。 有谁知道如何实现这一目标?

My combobox is bound to 3 properties (IsVisited, Address and City):

<ComboBox Height="100" HorizontalAlignment="Left" Margin="100,25,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="300"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsVisited}" Width="150" /> <TextBlock Text="{Binding Address}" Width="100" /> <TextBlock Text="{Binding City}" Width="100" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

I want to create a wpf combo box with drop down containing two columns

IsVisited (checkbox) City (string)

The selected textbox portion of the combobox should only display the 'Address'. Does any one know how this can be achieved?

最满意答案

我已经复制了你的问题,我已经完成了你的任务。

这是初始视图。

在此处输入图像描述

然后选择凯格拉,

在此处输入图像描述

我想这就是你需要的。

定义ComboBoxItem的view model 。 (在我的例子中,我创建了一个名为MyComboBoxItem的类,包括属性,即HasVisited, Address, City, TextBoxValue )

在主视图模型中,定义一个property来绑定ComboBox ItemsSource (我定义了一个名为ComboBoxItems的属性)和另一个属性来绑定ComboBox的SelectedItem (我定义了一个名为SelectedComboBoxItem的属性)。

在选择更改方法中,使用逻辑来设置texbox。

这是我的XAML

<Window x:Class="ComboboxSelectedItemChange.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:vm="clr-namespace:ComboboxSelectedItemChange.ViewModels" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.DataContext> <vm:MainViewModel /> </Grid.DataContext> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="40" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <Label Grid.Column="0" Grid.Row="0" Content="Combo box test" /> <ComboBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ComboBoxItems}" SelectedItem="{Binding SelectedComboBoxItem}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding HasVisited}" Width="30" /> <TextBlock Text="{Binding TextBoxValue}" Width="100" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding ChangeSelectionCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox> </Grid>

这是我的Main View model ,它绑定到上面的视图

public class MainViewModel : ViewModelBase
{
    #region Declarations

    private ObservableCollection<MyComboBoxItemViewModel> comboBoxItems;
    private MyComboBoxItemViewModel selectedComboBoxItem;

    private ICommand changeSelectionCommand;
    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the combo box items.
    /// </summary>
    /// <value>The combo box items.</value>
    public ObservableCollection<MyComboBoxItemViewModel> ComboBoxItems
    {
        get
        {
            return comboBoxItems;
        }
        set
        {
            comboBoxItems = value;
            NotifyPropertyChanged("ComboBoxItems");
        }
    }

    /// <summary>
    /// Gets or sets the selected combo box item.
    /// </summary>
    /// <value>The selected combo box item.</value>
    public MyComboBoxItemViewModel SelectedComboBoxItem
    {
        get
        {
            return selectedComboBoxItem;
        }
        set
        {
            selectedComboBoxItem = value;
            NotifyPropertyChanged("SelectedComboBoxItem");
        }
    }
    #endregion

    #region Commands

    /// <summary>
    /// Gets the change selection command.
    /// </summary>
    /// <value>The change selection command.</value>
    public ICommand ChangeSelectionCommand
    {
        get
        {
            if (changeSelectionCommand == null)
            {
                changeSelectionCommand = new RelayCommand(param => this.ChangeSelection(),
                    null);
            }
            return changeSelectionCommand;
        }
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="MainViewModel"/> class.
    /// </summary>
    public MainViewModel()
    {
        //Add some dummy data

        this.ComboBoxItems = new ObservableCollection<MyComboBoxItemViewModel>();

        MyComboBoxItemViewModel item1 = new MyComboBoxItemViewModel();
        item1.HasVisited = false;
        item1.Address = "123, Matara";
        item1.City = "Matara";
        item1.TextBoxValue = item1.City;
        this.ComboBoxItems.Add(item1);

        MyComboBoxItemViewModel item2 = new MyComboBoxItemViewModel();
        item2.HasVisited = false;
        item2.Address = "125, Colombo";
        item2.City = "Colombo";
        item2.TextBoxValue = item2.City;
        this.ComboBoxItems.Add(item2);

        MyComboBoxItemViewModel item3 = new MyComboBoxItemViewModel();
        item3.HasVisited = false;
        item3.Address = "465, Kegalle";
        item3.City = "Kegalle";
        item3.TextBoxValue = item3.City;
        this.ComboBoxItems.Add(item3);

        this.SelectedComboBoxItem = item2;

        this.ComboBoxItems.Where(item => item == this.SelectedComboBoxItem).First().TextBoxValue = this.SelectedComboBoxItem.Address;
    }

    #endregion

    #region Private Methods

    /// <summary>
    /// Changes the selection.
    /// </summary>
    private void ChangeSelection()
    {
        foreach (var comboBoxitem in this.ComboBoxItems)
        {
            this.ComboBoxItems.Where(item => item == comboBoxitem).First().TextBoxValue = comboBoxitem.City;
        }

        this.ComboBoxItems.Where(item => item == this.SelectedComboBoxItem).First().TextBoxValue = this.SelectedComboBoxItem.Address;
        this.ComboBoxItems.Where(item => item == this.SelectedComboBoxItem).First().HasVisited = true;
    }

    #endregion
}
 

这是MyComboBoxItemViewModel

public class MyComboBoxItemViewModel : ViewModelBase
{ 
    #region Declarations

    private bool hasVisited;
    private string address;
    private string city;
    private string textBoxValue;

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets a value indicating whether this instance has visited.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance has visited; otherwise, <c>false</c>.
    /// </value>
    public bool HasVisited
    {
        get
        {
            return hasVisited;
        }
        set
        {
            hasVisited = value;
            NotifyPropertyChanged("HasVisited");
        }
    }

    /// <summary>
    /// Gets or sets the address.
    /// </summary>
    /// <value>The address.</value>
    public string Address
    {
        get
        {
            return address;
        }
        set
        {
            address = value;
            NotifyPropertyChanged("Address");
        }
    }

    /// <summary>
    /// Gets or sets the city.
    /// </summary>
    /// <value>The city.</value>
    public string City
    {
        get
        {
            return city;
        }
        set
        {
            city = value;
            NotifyPropertyChanged("City");
        }
    }

    /// <summary>
    /// Gets or sets the text box value.
    /// </summary>
    /// <value>The text box value.</value>
    public string TextBoxValue
    {
        get
        {
            return textBoxValue;
        }
        set
        {
            textBoxValue = value;
            NotifyPropertyChanged("TextBoxValue");
        }
    }

    #endregion
}

I have reproduced your problem and I have achieved your task.

This is the initial view.

enter image description here

Then after selecting Kegalla,

enter image description here

I think this is what you need.

Define a view model for the ComboBoxItem. (In my case I created a class called MyComboBoxItem and include properties namely HasVisited, Address, City, TextBoxValue)

In your main view model, define a property to bind the ComboBox ItemsSource (I defined a property called ComboBoxItems) and another property to bind the SelectedItem of the ComboBox (I defined a property called SelectedComboBoxItem).

In the selection change method, implment your logic to set the texbox.

Here is my XAML

<Window x:Class="ComboboxSelectedItemChange.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:vm="clr-namespace:ComboboxSelectedItemChange.ViewModels" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.DataContext> <vm:MainViewModel /> </Grid.DataContext> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="40" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <Label Grid.Column="0" Grid.Row="0" Content="Combo box test" /> <ComboBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ComboBoxItems}" SelectedItem="{Binding SelectedComboBoxItem}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding HasVisited}" Width="30" /> <TextBlock Text="{Binding TextBoxValue}" Width="100" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding ChangeSelectionCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox> </Grid>

And this is my Main View model which binds to the above view

public class MainViewModel : ViewModelBase
{
    #region Declarations

    private ObservableCollection<MyComboBoxItemViewModel> comboBoxItems;
    private MyComboBoxItemViewModel selectedComboBoxItem;

    private ICommand changeSelectionCommand;
    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the combo box items.
    /// </summary>
    /// <value>The combo box items.</value>
    public ObservableCollection<MyComboBoxItemViewModel> ComboBoxItems
    {
        get
        {
            return comboBoxItems;
        }
        set
        {
            comboBoxItems = value;
            NotifyPropertyChanged("ComboBoxItems");
        }
    }

    /// <summary>
    /// Gets or sets the selected combo box item.
    /// </summary>
    /// <value>The selected combo box item.</value>
    public MyComboBoxItemViewModel SelectedComboBoxItem
    {
        get
        {
            return selectedComboBoxItem;
        }
        set
        {
            selectedComboBoxItem = value;
            NotifyPropertyChanged("SelectedComboBoxItem");
        }
    }
    #endregion

    #region Commands

    /// <summary>
    /// Gets the change selection command.
    /// </summary>
    /// <value>The change selection command.</value>
    public ICommand ChangeSelectionCommand
    {
        get
        {
            if (changeSelectionCommand == null)
            {
                changeSelectionCommand = new RelayCommand(param => this.ChangeSelection(),
                    null);
            }
            return changeSelectionCommand;
        }
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="MainViewModel"/> class.
    /// </summary>
    public MainViewModel()
    {
        //Add some dummy data

        this.ComboBoxItems = new ObservableCollection<MyComboBoxItemViewModel>();

        MyComboBoxItemViewModel item1 = new MyComboBoxItemViewModel();
        item1.HasVisited = false;
        item1.Address = "123, Matara";
        item1.City = "Matara";
        item1.TextBoxValue = item1.City;
        this.ComboBoxItems.Add(item1);

        MyComboBoxItemViewModel item2 = new MyComboBoxItemViewModel();
        item2.HasVisited = false;
        item2.Address = "125, Colombo";
        item2.City = "Colombo";
        item2.TextBoxValue = item2.City;
        this.ComboBoxItems.Add(item2);

        MyComboBoxItemViewModel item3 = new MyComboBoxItemViewModel();
        item3.HasVisited = false;
        item3.Address = "465, Kegalle";
        item3.City = "Kegalle";
        item3.TextBoxValue = item3.City;
        this.ComboBoxItems.Add(item3);

        this.SelectedComboBoxItem = item2;

        this.ComboBoxItems.Where(item => item == this.SelectedComboBoxItem).First().TextBoxValue = this.SelectedComboBoxItem.Address;
    }

    #endregion

    #region Private Methods

    /// <summary>
    /// Changes the selection.
    /// </summary>
    private void ChangeSelection()
    {
        foreach (var comboBoxitem in this.ComboBoxItems)
        {
            this.ComboBoxItems.Where(item => item == comboBoxitem).First().TextBoxValue = comboBoxitem.City;
        }

        this.ComboBoxItems.Where(item => item == this.SelectedComboBoxItem).First().TextBoxValue = this.SelectedComboBoxItem.Address;
        this.ComboBoxItems.Where(item => item == this.SelectedComboBoxItem).First().HasVisited = true;
    }

    #endregion
}
 

And this is the MyComboBoxItemViewModel

public class MyComboBoxItemViewModel : ViewModelBase
{ 
    #region Declarations

    private bool hasVisited;
    private string address;
    private string city;
    private string textBoxValue;

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets a value indicating whether this instance has visited.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance has visited; otherwise, <c>false</c>.
    /// </value>
    public bool HasVisited
    {
        get
        {
            return hasVisited;
        }
        set
        {
            hasVisited = value;
            NotifyPropertyChanged("HasVisited");
        }
    }

    /// <summary>
    /// Gets or sets the address.
    /// </summary>
    /// <value>The address.</value>
    public string Address
    {
        get
        {
            return address;
        }
        set
        {
            address = value;
            NotifyPropertyChanged("Address");
        }
    }

    /// <summary>
    /// Gets or sets the city.
    /// </summary>
    /// <value>The city.</value>
    public string City
    {
        get
        {
            return city;
        }
        set
        {
            city = value;
            NotifyPropertyChanged("City");
        }
    }

    /// <summary>
    /// Gets or sets the text box value.
    /// </summary>
    /// <value>The text box value.</value>
    public string TextBoxValue
    {
        get
        {
            return textBoxValue;
        }
        set
        {
            textBoxValue = value;
            NotifyPropertyChanged("TextBoxValue");
        }
    }

    #endregion
}

                    
                     
          

更多推荐

本文发布于:2023-07-04 11:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性   文本框   复选框   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!