开发者

WPF - Get combobox checked property from ListBox

I have a li开发者_C百科stbox, which is defined like so:

<ListBox ItemsSource="{Binding Source={x:Static local:ResourceCollection.resourceList}}" Height="143" HorizontalAlignment="Left" Margin="6,6,0,0" Name="assignmentLB" VerticalAlignment="Top" Width="287" FontSize="12" FontWeight="Normal" IsEnabled="True" Grid.Column="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox />
                <TextBlock Text="{Binding Content}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

How can I loop through this listbox and retrieve the TextBlock.Text value for only items whose Checkbox has been checked?

Also... how can I horizontally space out the checkbox from the textblock. Right now they are right next to each other with no spacing.


Add a boolean property to the class you are binding to (the same one with the Content property) and bind the CheckBox to it, like this:

<CheckBox IsChecked="{Binding IsSelected}"/>

Then you can simply loop through the resourceList and grab all the items that have IsSelected set to true, like this:

resourceList.Where(r => r.IsSelected);

As for the horizontal spacing, you just need to supply a Margin to either the CheckBox or the TextBlock, or both. A margin of 5,0 on the TextBlock should be all you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜