hei14 Skrevet 2. november 2022 Del Skrevet 2. november 2022 (endret) 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 <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" HorizontalAlignment="Left" FontSize="20" Margin="0 0 5 5"> buss størelse </TextBlock> <StackPanel> <ListView x:Name="MyListView" ItemsSource="{Binding Items}" ></ListView> <Button Content="Create View" Click="CreateViewButtonClicked"></Button> </StackPanel> </Grid> ShellViewModel + modeler public class ShellViewModel : Conductor<Object> { private IEventAggregator events; public ShellViewModel(IEventAggregator events) { events = _events; events.SubscribeOnPublishedThread(this); Dictionary<string, bool> test5 = new Dictionary<string, bool>(); foreach (FlagDisplayModel item in flaglist) { test5.Add(item.FlagNavn, false); } Items = new ObservableCollection<MyDataClass> { }; } 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<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; } } public class tranfurn { public tranfurn (List<FlagDisplayModel> myData) { this.MyData = myData; } public List<FlagDisplayModel> MyData { get; private set; } } } takk for alle svart test-listvi.zip Endret 15. november 2022 av hei14 Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå