c# - 如何将用户控件复选框绑定(bind)到列表框中

标签 c# wpf xaml mvvm

我编写了自己的复选框控件。这个复选框,我使用 MVVM 模式放在列表框中。此用户控件有自己的类、 View 模型和 xaml View 。

这是一个类:

public class MultiSelectListBox
{
    public bool IsChecked { get; set; }
    public string Text { get; set; }
}

用户控件的 View 模型:

public partial class VMMultiSelectListBox : ViewModelBase
{
    private bool _isChecked;
    private string _text;

    public VMMultiSelectListBox()
    {

    }

    public VMMultiSelectListBox(MultiSelectListBox.BusinnesModel.MultiSelectListBox item)
    {
        IsChecked = item.IsChecked;
        Text = item.Text;
    }

    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; NotifyPropertyChanged("IsChecked"); }
    }

    public string Text
    {
        get { return _text; }
        set { _text = value; NotifyPropertyChanged("Text"); }
    }           
}

这是 xaml:

<UserControl x:Class="MES.UserControls.MultiSelectListBox.UCMultiSelectListBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MES.UserControls.MultiSelectListBox">    
    <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="{Binding Text, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  />
</UserControl>

现在我想将这个 UserControl 绑定(bind)到我的 ListBox 中,它位于主窗体中。

这就是我在表单 xaml 中使用的内容。

<Expander x:Name="expanderProccesses" Header="Procesy" IsExpanded="{Binding IsExpanded}"  Grid.Column="1" Grid.Row="0" VerticalAlignment="Top" Margin="5,6,-30,0">
    <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding ProccessFilter}" SelectedItem="{Binding SelectedProcess, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ucLb:UCMultiSelectListBox/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Expander>

最后是这个表单的 View 模型。

public VMMultiSelectListBox SelectedProcess
{
    get { return _selectedProccess; }
    set { 
        _selectedProccess = value;                       
        NotifyPropertyChanged("SelectedProcess");
        NotifyPropertyChanged("ProccessFilter");                   
        }
} 

 public ObservableCollection<VMMultiSelectListBox> ProccessFilter
 {
    get { return _proccesFilter; }
    set { _proccesFilter = value;       NotifyPropertyChanged("ProccessFilter");}
}

我做错了什么。在 selectedProcces 中,它总是在 getter 中跳跃,而不是在我需要的 setter 中跳跃。我不完全知道为什么。

最佳答案

我认为您可以在更标准的上下文中通过在 ItemContainerStyle 中绑定(bind) IsSelected 属性并使用 CheckBoxItemTemplate 中:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectionMode="Extended" ItemsSource="{Binding ProccessFilter}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Text}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding IsChecked, Mode=TwoWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

请注意您应该设置SelectionMode="Extended"

希望对您有所帮助。

关于c# - 如何将用户控件复选框绑定(bind)到列表框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42572160/

相关文章:

c# - 在 C# 中评估 sql 语句的最佳方法是什么?

wpf - 如何将 ComboBox 与 DataTable 绑定(bind)

android - Xamarin.Forms 滑动手势识别器

wpf - 使用 Visual Studio 2017 在 WPF 应用程序中创建功能区

c# - .NET WCF Json 反序列化 Dictionary<int, int>

C#设置打印机

c# - Visual C# 使用链接将项目添加到 contextMenuStrip

c# - 如何从 CollectionViewSource 获取项目数?

c# - WPF 工具包 DataGrid 显示字段,即使可浏览属性设置为 false

c# - 如何删除应用磁贴的文本?