2017-03-03 186 views
2

我寫我自己的複選框控件。這個複選框,我使用MVVM模式放入列表框。這個用戶控件有它自己的類,視圖模型和xaml視圖。如何將用戶控件複選框綁定到列表框

這裏是一個類:

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

視圖模型的用戶控件:

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> 

現在我想這個用戶控件綁定我的列表框裏面,它位於主要形式。

這是我在我的表單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> 

最後一件事是這種形式的視圖模型。

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中,這是我需要的。我不完全知道爲什麼。

+0

你想要達到甚麼**完全**?看起來你正在重新發明輪子。你不必做一個UserControl。只需將DataTemplate模板作爲複選框並執行相應的綁定 – lokusking

+0

我希望實現的目標是在另一個列表框中使用用戶控件。我不想編寫數百個數據模板,只有一個使用控件,並且此用戶控件充滿了存儲庫中的數據。 (平均含量)。我認爲整個概念是好的,但是綁定有問題。 –

+0

你好,謝謝你的回答,我真的在嘗試,所以我會讓你知道:) –

回答

0

我的事情你正在嘗試做可以在一個更標準的背景下實現的,通過在ItemContainerStyle結合IsSelected財產,並在ItemTemplate使用CheckBox

<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"

希望它有幫助。

+0

謝謝你的幫助隊友。它實際上在二傳手中跳躍,但僅限於我選擇的一個項目。如果我從列表框中選擇另一個項目,它不會在setter中跳躍,只是我選擇的第一個項目。 :/ –

相關問題