我寫我自己的複選框控件。這個複選框,我使用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中,這是我需要的。我不完全知道爲什麼。
你想要達到甚麼**完全**?看起來你正在重新發明輪子。你不必做一個UserControl。只需將DataTemplate模板作爲複選框並執行相應的綁定 – lokusking
我希望實現的目標是在另一個列表框中使用用戶控件。我不想編寫數百個數據模板,只有一個使用控件,並且此用戶控件充滿了存儲庫中的數據。 (平均含量)。我認爲整個概念是好的,但是綁定有問題。 –
你好,謝謝你的回答,我真的在嘗試,所以我會讓你知道:) –