Â
jeg bruker caliburn micro . hvordan kan jeg akses properties i Shellviewmodel fra ShellXview.xaml.cs eller sende inn informasjon mellom dem?
ShellView.xaml.cs
public partial class ShellView : Window { public ShellView() { InitializeComponent(); DataContext = this; } private List<FlagDisplayModel> _flags = new List<FlagDisplayModel>(); public List<FlagDisplayModel> flags { get { return _flags; } set { _flags = value;} } private List<SignalDisplayModel> _signals = new List<SignalDisplayModel>(); public List<SignalDisplayModel> signals { get { return _signals; } set { _signals = value; } } public void InitializeColume() { var gridView = new GridView(); gridView.AllowsColumnReorder = false; TextBlock textBlockr = new TextBlock(); textBlockr.Text = "Oppcode"; textBlockr.RenderTransformOrigin = new Point(0.5, 0.5); textBlockr.LayoutTransform = new RotateTransform() { Angle = 90 }; textBlockr.HorizontalAlignment = HorizontalAlignment.Center; GridViewColumn Oppcode = new GridViewColumn { Header = textBlockr, DisplayMemberBinding = new Binding("Oppcode") }; gridView.Columns.Add(Oppcode); TextBlock textBlocke = new TextBlock(); textBlocke.Text = "CodeNavn"; textBlocke.RenderTransformOrigin = new Point(0.5, 0.5); textBlocke.LayoutTransform = new RotateTransform() { Angle = 90 }; textBlocke.HorizontalAlignment = HorizontalAlignment.Center; GridViewColumn CodeNavn = new GridViewColumn { Header = textBlocke, DisplayMemberBinding = new Binding("CodeNavn") }; gridView.Columns.Add(CodeNavn); foreach (FlagDisplayModel p in flags) { FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox)); checkBox.Name = $"{p.FlagNavn}"; checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding("Name[" + $"{p.FlagNavn}" + "]")); // binding her DataTemplate dataTemplate = new DataTemplate() { VisualTree = checkBox }; TextBlock textBlock = new TextBlock(); textBlock.Text = $"{p.FlagNavn}"; textBlock.RenderTransformOrigin = new Point(0.5, 0.5); textBlock.LayoutTransform = new RotateTransform() { Angle = 90 }; textBlock.HorizontalAlignment = HorizontalAlignment.Center; GridViewColumn check = new GridViewColumn { Header = textBlock, CellTemplate = new DataTemplate { VisualTree = checkBox } }; gridView.Columns.Add(check); } GridViewColumn emti = new GridViewColumn { Header = " ", Width = 20, DisplayMemberBinding = new Binding("emti") }; gridView.Columns.Add(emti); foreach (SignalDisplayModel p in signals) { FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox)); checkBox.Name = $"{p.SignalNavn}"; checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding("Name[" + $"{p.SignalNavn}" + "]")); // binding her DataTemplate dataTemplate = new DataTemplate() { VisualTree = checkBox }; TextBlock textBlock = new TextBlock(); textBlock.Text = $"{p.SignalNavn}"; textBlock.RenderTransformOrigin = new Point(0.5, 0.5); textBlock.LayoutTransform = new RotateTransform() { Angle = 90 }; textBlock.HorizontalAlignment = HorizontalAlignment.Center; GridViewColumn check = new GridViewColumn { Header = textBlock, CellTemplate = new DataTemplate { VisualTree = checkBox } }; gridView.Columns.Add(check); } MyListView.View = gridView; } private void SetItemsSource() { Dictionary<string, bool> test5 = new Dictionary<string, bool>(); foreach (FlagDisplayModel item in flags) { test5.Add(item.FlagNavn, false); } foreach (SignalDisplayModel signal in signals) { test5.Add(signal.SignalNavn, false); } Items = new ObservableCollection<MyDataClass>{ }; Items.Add(new MyDataClass { Oppcode = "0", CodeNavn = "pall", Name = test5 }); Items.Add(new MyDataClass { Oppcode = "1", CodeNavn = "pal", Name = test5 }); Items.Add(new MyDataClass { Oppcode = "2", CodeNavn = "paffffffffffffl", Name = test5 }); } private void CreateViewButtonClicked(object sender, RoutedEventArgs e) { flags.Add(new FlagDisplayModel { id = 1, FlagNavn = "tedg", nr = 0 }); flags.Add(new FlagDisplayModel { id = 2, FlagNavn = "test", nr = 0 }); signals.Add(new SignalDisplayModel { id = 1, SignalNavn = "Incre", ControlCode = "A15", RomNr = 0 }); InitializeColume(); SetItemsSource(); MyListView.ItemsSource = Items; } public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register( "Items", typeof(ObservableCollection<MyDataClass>), typeof(ShellView), new PropertyMetadata(default(ObservableCollection<MyDataClass>))); public ObservableCollection<MyDataClass> Items { get { return (ObservableCollection<MyDataClass>)GetValue(ItemsProperty); } set { SetValue(ItemsProperty, value); } } }
ShellView.xaml
Height="auto"/> Height="auto"/> Height="*"/> Grid.Row="0" HorizontalAlignment="Left" FontSize="20" Margin="0 0 5 5"> buss størelse x:Name="MyListView" ItemsSource="{Binding Items}" >
ShellViewModel + modeler
public class ShellViewModel : Conductor<Object> { public ShellViewModel(IEventAggregator events) { Dictionary<string, bool> test5 = new Dictionary<string, bool>(); foreach (FlagDisplayModel item in flaglist) { test5.Add(item.FlagNavn, false); } Items = new ObservableCollection<MyDataClass> { }; } private List<FlagDisplayModel> _flaglist = new List<FlagDisplayModel>(); public List<FlagDisplayModel> flaglist { get { return _flaglist; } private set { _flaglist = value; } } private ObservableCollection<MyDataClass> _Items = new ObservableCollection<MyDataClass>(); public ObservableCollection<MyDataClass> Items { get { return _Items; } set { _Items = value; NotifyOfPropertyChange(() => Items); } } } public class MyDataClass { public string Oppcode { get; set; } public string CodeNavn { get; set; } public string emti { get; set; } public Dictionary<string, bool> Name { get; set; } = new Dictionary<string, bool>(); } public class FlagDisplayModel { public int id { get; set; } public string FlagNavn { get; set; } public int nr { get; set; } } public class SignalDisplayModel { public int id { get; set; } public string SignalNavn { get; set; } public string ControlCode { get; set; } public int RomNr { get; set; } }
jeg prøvt og bruke notifyofpropertychange og event aggregatorÂ