משיב מוביל
itemcontainerstyle ב-win8

שאלה
-
ראשית ל רציתי להודות על ה"בלוג" המוצלח נעזרתי בו רבות! התשובות ממש קולעות
רציתי לשאול:
יש לי רשימה של TOP ו-LEFT בקוד
אני רוצה לייצג אותם ב-XAML בעזרת אליפסות שכל אליפסה תעמוד במקום המתאים על ה-CANVAS בהתאם ל-LEFTו-TOL שאליהם היא BINDING במודליצרתי קוד זה תוך שימוש ב -itemcontainerstyleהקוד עובד מצוין ב-win7 אולם ברגע שאני מעבירה את הקוד ל-win8 הוא לא פועל וממקם את כל העיגולים בנקודה 0,0זה נראה כאילו הוא לא מזהה את ה-itemcontainerstyleחיפשנו המון חומר ברשת ולא מצאנו תשובהנשמח מאד להענות בהקדםתודה !!!מצורף הקוד לדוגמאcode xaml:<ItemsControl ItemsSource="{Binding}" Background="Red"><ItemsControl.ItemTemplate><DataTemplate><Grid><Ellipse Width="30" Height="30" Fill="Red"></Ellipse></Grid></DataTemplate></ItemsControl.ItemTemplate><ItemsControl.ItemsPanel><ItemsPanelTemplate><Canvas></Canvas></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter" ><Setter Property="Canvas.Left" Value="{Binding Path=Left}"></Setter></Style></ItemsControl.ItemContainerStyle> code bheind:public class Model:INotifyPropertyChanged{private double left;public double Left{get { return left; }set { left = value; }}private double top;public double Top{get { return top; }set { top = value; }}public Model(double xx, double yy){Left = xx;Top = yy;}public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChange(string nameProperty){}}public sealed partial class MainPage : Page{private ObservableCollection<Model> list2;public ObservableCollection<Model> List2{get{return list2;}set { list2 = value; }}public MainPage(){List2 = new ObservableCollection<Model>() { new Model(30, 40), new Model(200, 200), new Model(100, 100), new Model(0, 0) };this.InitializeComponent();this.DataContext = List2;}protected override void OnNavigatedTo(NavigationEventArgs e) {}}יום חמישי 07 נובמבר 2013 20:03
תשובות
-
היי,
ב- Windows 8 Store App אין תמיכה ב- Data Binding מתוך Style כמו שקיים ב- WPF/SL5.
לכן יש דרכים עקיפות: attached properties, attached behaviors, custom control וכו'.מצ"ב דוגמה פשוטה עם פתרון attached property שלא דורש צד שלישי והפתרון הוא מאוד טריוויאלי, מצד שני מוגבל לפתרון ספציפי:
1. יש ליצור attached proerpty. להלן קוד:
public static class CanvasBindings { public static string GetLeftPath(DependencyObject obj) { return (string)obj.GetValue(LeftPathProperty); } public static void SetLeftPath(DependencyObject obj, string value) { obj.SetValue(LeftPathProperty, value); } // Using a DependencyProperty as the backing store for LeftPath. This enables animation, styling, binding, etc... public static readonly DependencyProperty LeftPathProperty = DependencyProperty.RegisterAttached( "LeftPath", typeof(string), typeof(CanvasBindings), new PropertyMetadata(null, (d, e) => CreateBinding(d, Canvas.LeftProperty, (string)e.NewValue))); public static string GetTopPath(DependencyObject obj) { return (string)obj.GetValue(TopPathProperty); } public static void SetTopPath(DependencyObject obj, string value) { obj.SetValue(TopPathProperty, value); } // Using a DependencyProperty as the backing store for TopPath. This enables animation, styling, binding, etc... public static readonly DependencyProperty TopPathProperty = DependencyProperty.RegisterAttached( "TopPath", typeof(string), typeof(CanvasBindings), new PropertyMetadata(null, (d, e) => CreateBinding(d, Canvas.TopProperty, (string)e.NewValue))); private static void CreateBinding(DependencyObject target, DependencyProperty targetProperty, string sourcePath) { var binding = new Binding { Path = new PropertyPath(sourcePath), Mode = BindingMode.TwoWay }; BindingOperations.SetBinding(target, targetProperty, binding); } }
2. יש להפעיל את ה- attached property בתוך הסטייל של הרשימה. להלן קוד:
<ListBox ItemsSource="{Binding Artifacts}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <Canvas /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="local:CanvasBindings.LeftPath" Value="X" /> <Setter Property="local:CanvasBindings.TopPath" Value="Y" /> </Style> </ItemsControl.ItemContainerStyle> </ListBox>
להלן קישור לקוד מלא שמדגים את הרעיון:
בברכה,
תומר שמם
http://blogs.microsoft.co.il/blogs/tomershamam
CodeValue
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- הוצע כתשובה על-ידי תומר שמםModerator יום שני 11 נובמבר 2013 14:31
- נערך על-ידי תומר שמםModerator יום שני 11 נובמבר 2013 17:00 הוספת קישור לקוד
- סומן כתשובה על-ידי Eran Sharvit יום שלישי 12 נובמבר 2013 00:32
יום שני 11 נובמבר 2013 14:23מנחה דיון
כל התגובות
-
תודה על הסבלנות. בקרוב מומחה הקהילה ינסה לעזור לכם.
מיקרוסופט מציעה שירות זה ללא תשלום, למטרת סיוע למשתמשים והעשרת הידע הקשור בטכנולוגיות ובמוצרים של מיקרוסופט. תוכן זה מתפרסם כפי שהוא והוא אינו מעיד על כל אחריות מצד מיקרוסופט.
יום שני 11 נובמבר 2013 01:01 -
היי,
ב- Windows 8 Store App אין תמיכה ב- Data Binding מתוך Style כמו שקיים ב- WPF/SL5.
לכן יש דרכים עקיפות: attached properties, attached behaviors, custom control וכו'.מצ"ב דוגמה פשוטה עם פתרון attached property שלא דורש צד שלישי והפתרון הוא מאוד טריוויאלי, מצד שני מוגבל לפתרון ספציפי:
1. יש ליצור attached proerpty. להלן קוד:
public static class CanvasBindings { public static string GetLeftPath(DependencyObject obj) { return (string)obj.GetValue(LeftPathProperty); } public static void SetLeftPath(DependencyObject obj, string value) { obj.SetValue(LeftPathProperty, value); } // Using a DependencyProperty as the backing store for LeftPath. This enables animation, styling, binding, etc... public static readonly DependencyProperty LeftPathProperty = DependencyProperty.RegisterAttached( "LeftPath", typeof(string), typeof(CanvasBindings), new PropertyMetadata(null, (d, e) => CreateBinding(d, Canvas.LeftProperty, (string)e.NewValue))); public static string GetTopPath(DependencyObject obj) { return (string)obj.GetValue(TopPathProperty); } public static void SetTopPath(DependencyObject obj, string value) { obj.SetValue(TopPathProperty, value); } // Using a DependencyProperty as the backing store for TopPath. This enables animation, styling, binding, etc... public static readonly DependencyProperty TopPathProperty = DependencyProperty.RegisterAttached( "TopPath", typeof(string), typeof(CanvasBindings), new PropertyMetadata(null, (d, e) => CreateBinding(d, Canvas.TopProperty, (string)e.NewValue))); private static void CreateBinding(DependencyObject target, DependencyProperty targetProperty, string sourcePath) { var binding = new Binding { Path = new PropertyPath(sourcePath), Mode = BindingMode.TwoWay }; BindingOperations.SetBinding(target, targetProperty, binding); } }
2. יש להפעיל את ה- attached property בתוך הסטייל של הרשימה. להלן קוד:
<ListBox ItemsSource="{Binding Artifacts}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <Canvas /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="local:CanvasBindings.LeftPath" Value="X" /> <Setter Property="local:CanvasBindings.TopPath" Value="Y" /> </Style> </ItemsControl.ItemContainerStyle> </ListBox>
להלן קישור לקוד מלא שמדגים את הרעיון:
בברכה,
תומר שמם
http://blogs.microsoft.co.il/blogs/tomershamam
CodeValue
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- הוצע כתשובה על-ידי תומר שמםModerator יום שני 11 נובמבר 2013 14:31
- נערך על-ידי תומר שמםModerator יום שני 11 נובמבר 2013 17:00 הוספת קישור לקוד
- סומן כתשובה על-ידי Eran Sharvit יום שלישי 12 נובמבר 2013 00:32
יום שני 11 נובמבר 2013 14:23מנחה דיון