簡體   English   中英

如何將項目集合綁定到復選框列表框?

[英]How do you bind a collection of items to a list box of checkboxes?

抱歉,您的描述含糊不清,我想不出更好的方法來表達它。

假設我的ViewModel具有如下屬性:

public List<MyClass> SubSystems { get; set; }

和SubSystems類:

public class SubSystem
{
    public string Name { get; set; }

    public bool IsSelected { get; set; }
}

在視圖中,我想將SubSystems屬性綁定到復選框列表,其中CheckBox的IsChecked和Name屬性綁定到其各自的屬性IsChecked for IsSelected和Content for Name。

我知道我可以在XAML中創建一個ListBox,但是我不確定如何使用綁定和集合來完成此操作。

謝謝您的幫助!

編輯-

這是XAML:

<GroupBox Header="Sub-Systems" Grid.Column="0" Grid.Row="0" Margin="5">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="checkBox">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsSelected}" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <ListBox ItemTemplate="{StaticResource checkBox}" ItemsSource="{Binding SubSystems}" />
    </Grid>
</GroupBox>

編輯#2-

為了澄清起見,所有示例都填充在框中,但是沒有一個示例在設置器的斷點處中斷。

這個怎么樣:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked={Binding IsSelected, Mode=TwoWay} /><TextBlock Text={Binding Name} />
    </StackPanel>
</DataTemplate>

修改ListBox.ItemTemplate以使用復選框,然后將CheckBox.IsChecked綁定到SubSystem.IsSelected並將CheckBox.Content綁定到SubSystem.Name:

XAML:

<ListBox ItemsSource="{Binding}">
  <ListBox.ItemTemplate>
   <DataTemplate>
    <CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Name}" Margin="5" Focusable="False" />
   </DataTemplate>
  </ListBox.ItemTemplate>
 </ListBox>

C#:

private void window1_Loaded(object sender, RoutedEventArgs e)
{
 this.SubSystems = new List<SubSystem>();

 this.SubSystems.Add(new SubSystem() { Name = "SubSystem 1", IsSelected = false });
 this.SubSystems.Add(new SubSystem() { Name = "SubSystem 2", IsSelected = false });
 this.SubSystems.Add(new SubSystem() { Name = "SubSystem 3", IsSelected = true });
 this.SubSystems.Add(new SubSystem() { Name = "SubSystem 4", IsSelected = false });
 this.SubSystems.Add(new SubSystem() { Name = "SubSystem 5", IsSelected = true });

 this.DataContext = this.SubSystems;
}

並確保將Focusable =“ False”設置為CheckBoxes,否則您的用戶將能夠使用它們來制表。

編輯:

同樣,從添加的內容中,您可能會缺少ElementName屬性(如果SubSystems不是窗口的DataContext,則需要使用ElementName綁定屬性指定SubSystems屬性來自何處):

 <ListBox ItemTemplate="{StaticResource checkBox}" ItemsSource="{Binding ElementName=window1, Path=SubSystems}" />

我認為您可能需要ItemsControl而不是ListBox ListBoxes假定您要選擇一個SubSystem但實際上,您只想使用數據模板來安排項目:

<ItemsControl ItemsSource="{Binding SubSystems}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Checkbox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Name}" />
    </DataTemplate>
  <ItemsControl.ItemTemplate>
</ItemsControl>

你的意思是這樣嗎?

子系統類

public class SubSystem : INotifyPropertyChanged
{
    private string mName;
    private Boolean mIsSelected = false;

    public SubSystem()
    {
    }

    public SubSystem(string name, Boolean isSelected)
    {
        this.Name = name;
        this.IsSelected = isSelected;
    }

    public string Name
    {
        get { return mName; }
        set
        {
            if (mName != value)
            {
                mName = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }

    public Boolean IsSelected
    {
        get { return mIsSelected; }
        set
        {
            if (mIsSelected != value)
            {
                mIsSelected = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

視圖模型

ObservableCollection<SubSystem> mSubSystems = new ObservableCollection<SubSystem>();
public ObservableCollection<SubSystem> SubSystems
{
    get { return mSubSystems; }
    set { mSubSystems = value; }
}

視圖

<ListBox x:Name="lstSubsystems" ItemsSource="{Binding SubSystems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsSelected}">
                <ContentPresenter Content="{Binding Name}"  />
            </CheckBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

希望有幫助,Wts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM