Jeg bruker caliburn micro.
Jeg er ny i programmering og selvlært.
Hvordan kan jeg få tilgang til proberti i viewmodel fra view.xaml.cs eller sende informasjon mellom dem?
Jeg prøvde å implementere ved hjelp av event aggregator, programmet vil ikke starte.
Her er koden jeg bruker:
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 Task HandleAsync(tranfurn message, CancellationToken cancellationToken) { flags = message.MyData; return Task.CompletedTask; } 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 { private IEventAggregator events; public ShellViewModel(IEventAggregator events) { events = _events; events.SubscribeOnPublishedThread(this); Dictionary, bool> test5 = new Dictionary , bool>(); foreach (FlagDisplayModel item in flaglist) { test5.Add(item.FlagNavn, false); } Items = new ObservableCollection { }; } protected override void OnViewLoaded(object view) { flaglist.Add(new FlagDisplayModel { id = 1, FlagNavn = "tedg", nr = 0 }); flaglist.Add(new FlagDisplayModel { id = 2, FlagNavn = "test", nr = 0 }); base.OnViewLoaded(view); events.PublishOnUIThreadAsync(new tranfurn(flaglist)); } private List _flaglist = new List (); public List flaglist { get { return _flaglist; } private set { _flaglist = value; } } private ObservableCollection _Items = new ObservableCollection (); public ObservableCollection 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 , bool> Name { get; set; } = new Dictionary , 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; } } public class tranfurn { public tranfurn (List myData) { this.MyData = myData; } public List MyData { get; private set; } } }
takk for alle svart