שואל
checkboxlist databinding

שאלה
-
I want to have two lists:
-
ListView with the columns:
a. checkbox.
b. User Name.
c. User Email.
-
Listbox to show the Email field of the selected items in the listview.
All the Email items in the listbox shuld be binded to property:
List<string> Emails
Can anyone help me with that ?
Thanks a lot
יום שני 11 מרץ 2013 11:38 -
כל התגובות
-
היי,
קימצת בפרטים לכן קשה לעזור לך.
אבל אם אתה מתכוון לממש GridView בתוך ListView כאן יש דוגמא איך לעשות את זה.
http://tech.pro/tutorial/742/wpf-tutorial-using-the-listview-part-1
אם אתה צריך עריכה אז תשתמש ב-DataGrid (קיים ב-WPF 4 ומעלה) שנותן לך הרבה יותר יכולות אך גם יותר מרוכב.
וגם אתה יכול להשתמש בקונטרולים (בתשלום) של חברות כמו טלריק, אינפרגיסטיקס וכו'.
מומלץ לך להשתמש ב-MVVM כי אז זה יקל עליך שאתה עושה DataBinding.
בכל מקרה נניח שהמודל (למשטרת ה-MVVM, אני מפשט בכוונה תחילה) שלך נראה ככה:
class Customer
{
public bool IsChecked { get; set;}
public string Name { get; set;}
public string Email { get; set;}
}
אם יש לך רשימה של כאלה אתה תוכל לעשות Binding לעמודות השונות בקלות.
לגבי השאלה השנייה אתה תצטרך להשתמש בעמודה של ComboBox ולעשות Binding של ה-ItemsSource ל-ViewModel הראשי.
מכיוון שאתה תהיה בסקופ של Item בודד, עומדות בפניך כמה אופציות, הפחות סבירה זה שכל Item יחזיק את הרשימה של ה-Emails. האופציות העדיפות זה לגרום ל-Binding לבחור ב-Source אחר, אפשר לעשות את זה ע"י שימוש ב-ElementName או FindAncestor (קיים באופן טבעי רק ב-WPF).
בצלחה
Ariel Ben Horesh | twitter: @arielbh blog: http://blogs.microsoft.co.il/blogs/arielbh/
יום שלישי 12 מרץ 2013 08:37 -
הי אריאל,
תודה על התשובה המפורטת.
אפרט יותר:
יש לי listview שמכיל את
a. checkbox.
b. User Name.
c. User Email
וגם יש לי listbox שבו אני רוצה להראות את השדה של ה-email. מצורף הקוד.
אני רוצה לבחור ב-listview את ה-checkbox ואז שיופע לי ב-listbox ה-email של המשתמש והוא אוטומאטית יכנס ל-property מסוג list של string. וגם הפוך. אם עשיתי uncheck, ה-email ירד מה-list וגם לא יראה ב-listbox. אני פשוט לא יודע איך לעשות את ה-binding בין ה-list לבין ה-listbox ובין ה-checkbox ל-listbox items.
המון תודה.
<Label Grid.Row="1" Style="{StaticResource Header3Label}" Content="To:"></Label> <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal"> <GroupBox Margin="2" Header="Email Recipients" Width="200"> <ListBox Name="_lstSelecedUsers" ItemsSource="{Binding ElementName=_lstUsers, Path=SelectedItems}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Email}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </GroupBox> <Canvas Margin="5" Width="60" Height="20"> <Line X1="10" Y1="10" X2="30" Y2="10" Stroke="Black" StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/> <Line X1="10" Y1="10" X2="15" Y2="15" Stroke="Black" StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/> <Line X1="10" Y1="10" X2="15" Y2="5" Stroke="Black" StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/> </Canvas> <controls:SortableListView x:Name="_lstUsers" Margin="2" Background="Transparent" HorizontalContentAlignment="Center" ItemsSource="{Binding Source={x:Static mngr:ProjectMngr.Instance}, Path=WorkingProject.Users, Mode=TwoWay}"> <controls:SortableListView.View> <GridView> <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox Name="_chbUserSelected" IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="User Name" DisplayMemberBinding="{Binding UserName}" controls:SortableListView.SortPropertyName="UserName"/> <GridViewColumn Header="Email" DisplayMemberBinding="{Binding Email}" controls:SortableListView.SortPropertyName="Email"/> </GridView> </controls:SortableListView.View> </controls:SortableListView> </StackPanel>
יום רביעי 13 מרץ 2013 08:40 -
היי,
המפתח לפתרון זה להגיב לשינוי של IsSelected, אתה יכול לעשות Binding ל- Visibility של העמודה (יחד עם BooleanToVisiblityConverter או אם אתה יכול להוסיף property חדש מסוג Visibility באובייקט שעושים לו Binding.
Ariel Ben Horesh | twitter: @arielbh blog: http://blogs.microsoft.co.il/blogs/arielbh/
יום רביעי 13 מרץ 2013 20:50