简体   繁体   中英

set checkbox in a combox to checked wpf

I have created a custom wpf control- essentially a combox with checkbox. The combox all successfully bound to a list of item.

It's my xaml code.

<ComboBox Height="28" HorizontalAlignment="Left" Margin="106,7,0,0" Name="comboBox1" VerticalAlignment="Top" Width="174" ItemsSource="{Binding Names}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Name="ckabc" Content="{Binding}" CommandParameter="{Binding}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox> 

My code is here like this:

private List<string> names;
public List<string> Names
    {
        get { return names; }
        set
        {
            names = value;
            this.OnPropertyChanged(new PropertyChangedEventArgs("Names"));
        }
    }
Names = new List<string> { "Sensor Not Connected", "Poor Signal Quality", "Excessive Light", "PreAmp Not Connected", "Replace Sensor", "Interference Detected", "Sensor Unusable", "Sensor Change" };
        this.OnPropertyChanged(new PropertyChangedEventArgs("Names"));

I have created property for each list item:

public string SensorNotConnected
    {
        get
        {
            return Names.ElementAt(0);
        }
        set
        {
            this.emuObj.SensorNotConnected(Convert.ToBoolean(value), channelIndex);
        }
    }

same way created property for other list item. My thinking is to bind Ischecked property of checkbox and iterate over. But how Can I do that. User can select one check box or multiple checkbox. Please provide some answer for this.

PS : I'm using MVVM model.

Here's a simple demo of how you could solve this. The solution uses Mvvm Light , however this is not necessary. Instead of having just a List<string> , you could create a class ("Name" in this example), which has a bool IsChecked property to which you can bind. See the line <CheckBox Grid.Column="1" Name="ckabc" IsChecked="{Binding IsChecked}"/> , this is where we bind the IsChecked property.

<Window x:Class="CustomControlWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
<Grid>
    <ComboBox Height="28" HorizontalAlignment="Left" Margin="106,7,0,0"  VerticalAlignment="Top" Width="174" ItemsSource="{Binding Names}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Description}"></TextBlock>
                    <CheckBox Grid.Column="1" Name="ckabc" IsChecked="{Binding IsChecked}"/>
                </Grid>

            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

public class Name
{
    public string Description { get; set; }
    public bool IsChecked { get; set; }
}

}

ViewModel:

 public class MainViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        Names = new List<Name>() { new Name { Description = "Name1", IsChecked = false }, new Name { Description = "Name2", IsChecked = false } };
    }

    private List<Name> _names;

    public List<Name> Names
    {
        get { return _names; }
        set
        {
            _names = value;
            RaisePropertyChanged(() => Names);
        }
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM