0

I hope I'm going about this the right way, but if I am not, then please advise.

I start off with a loop through a strongly typed collection:

foreach (var item in myItems)
            {
     //magic happens here
}

Inside the foreach loop, I'm adding items to a wrapPanel, based on the collection. As part of this, each item has a checkbox. Also each item in the collection has a unique id.

What I want is when the checkbox is clicked, it adds or removes an item from a List. But even ideally would be nice to have an isSelected property in an existing List.

So I was thinking of attaching an onclick event handler to the checkbox, and somehow attaching the id (hidden from UI) to the checkbox. then inside the onclick - handle things manually.

Is there a better way to do this?

EDIT: My existing code

foreach (var x in y)
            {
                Grid lbItem = new Grid();



                StackPanel sp = new StackPanel();
                sp.Width = 190;

                Border border = new Border();
                border.Padding = new Thickness(10); 
                border.BorderBrush = new SolidColorBrush(Colors.Gainsboro);
                border.BorderThickness = new Thickness(2);
                border.CornerRadius = new CornerRadius(15);

                CheckBox cb = new CheckBox();
                cb.Content = x.id; 
                cb.Padding = new Thickness(0,0,5,5); 
                StackPanel sptitle = new StackPanel();
                sptitle.Orientation  = Orientation.Horizontal;
                sptitle.Children.Add(cb); 

                TextBlock tbTitle = new TextBlock();
                tbTitle.Text = x.title;
                tbTitle.TextAlignment = TextAlignment.Justify;
                sptitle.Children.Add(tbTitle); 

                TextBlock tbShortDesc = new TextBlock();
                tbShortDesc.FontSize = 8; 
                tbShortDesc.Text = x.shortDesc; 
                Image imgDeal = new Image();
                imgDeal.Height = 180;
                imgDeal.Width = 180; 
                imgDeal.Source = new BitmapImage(new Uri(@"http://myurl/images/" + x.thumburl));

                sp.Children.Add(sptitle); 
                sp.Children.Add(imgDeal); 
                sp.Children.Add(tbShortDesc);

                border.Child = sp; 
                lbItem.Children.Add(border);

                DealsWrapPanel.Children.Add(lbItem);

                Rectangle rect = new Rectangle();
                rect.Width = 5;
                DealsWrapPanel.Children.Add(rect); 
            }
4

1 回答 1

1

好的。删除所有代码并重新开始。

这是在 WPF 中执行此操作的正确方法:

<ItemsControl ItemsSource="{Binding YourCollection}">
  <ItemsControl.ItemsPanel>
     <ItemsPanelTemplate>
        <WrapPanel/> <!-- This is your "DealsWrapPanel -->
     </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>

  <ItemsControl.ItemTemplate>
     <DataTemplate>
        <Grid Margin="0,0,5,0"> <!-- this is your LBItem, I added the margin because I realized you're using the Rectangle to separate items -->
             <Border Padding="10" BorderThickness="2"
                     BorderBrush="Gainsboro" CornerRadius="15">

                <StackPanel Width="190">
                    <StackPanel Orientation="Horizontal"> <!-- this is your sptitle -->
                        <CheckBox Padding="0,0,5,5" Content="{Binding id}" IsChecked="{Binding SomeBoolValue}" /> <!-- you need a boolean value to bind to -->
                        <TextBlock Text="{Binding title}" TextAlignment="Justify"/>

                        <Image Width="180" Height="180" Source="{Binding thumburl}"/> <!-- you need to append the full url string correctly before passing that to the UI -->

                        <TextBlock Text="{Binding shortdesc}" FontSize="8"/>
                    </StackPanel>
                </StackPanel>
             </Border>
         </Grid>
     </DataTemplate>
  </ItemsControl.ItemTemplate>
<ItemsControl>

(在此处键入 XAML,因此可能存在问题/错别字)

大多数时候,不需要在 WPF 的过程代码中操作 UI 元素。创建一个合适的 ViewModel 并让 UI 使用 DataBinding 创建自己。

还要记住UI 不是 Data,所以每当您需要从这些元素中“读取”属性时,您实际上需要从集合内的 Data Items 中“读取”属性,这些属性将被双向绑定到用户界面。

于 2013-08-09T01:25:16.043 回答