C# WPF Gridview Combobox Eintrag auswählen

Mystery1988

Ensign
Registriert
März 2015
Beiträge
204
Hallo zusammen,

ich hoffe ihr könnt mir irgendwie helfen, ich bin aktuell einfach zu doof um zu verstehen wie ich richtig ein Binding aufbaue oder eventuell komplett auf dem Holzweg bin.

Mein Ziel:
Ich versuche über eine Gridview für jede Zeile eine Combobox zu haben. Dieses funktioniert auch sehr gut.
Wenn ich nun ein neues Element zur Listview hinzufüge, möchte ich das ein Wert aus der Combobox gewählt wird.

Abstraktes Beispiel
Combobox liste = Exists, -, Add, Remove
Neues Element = In der Combobox soll z.B. Exists ausgewählt werden oder -.

Den Code habe ich auf das Notwendigste eingekürzt.

C#:
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net.Http.Headers;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net;
using ListViewItem = System.Windows.Controls.ListViewItem;
using CheckBox = System.Windows.Forms.CheckBox;
using ComboBox = System.Windows.Forms.ComboBox;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace vca_config_tool {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private readonly string currentScriptPath = AppDomain.CurrentDomain.BaseDirectory;
        private string zipFilePath = string.Empty;
        private string luaFilePath = string.Empty;
        private string zipPath = string.Empty;
        private List<LuaFile> luaAL = new List<LuaFile>();
        private List<LuaFile> checkedList = new List<LuaFile>();
        private List<LuaFile> unCheckedList = new List<LuaFile>();
        private ObservableCollection<LuaFile> currentTransmission = new ObservableCollection<LuaFile>();
        private string currentVCATransmissionFileString;
        private static WebClient wc = new WebClient();

        public MainWindow() {
            InitializeComponent();
            Console.WriteLine("Fin");

        }

        private void SelectZipButtonClick(object sender, RoutedEventArgs e) {
            // Read the current transmission file
            currentTransmission = GetAllUsedTransmissions();
        }

        /// <summary>
        /// Collect all current Transmission
        /// </summary>
        /// <returns> A list of the current transmission</returns>
        private ObservableCollection<LuaFile> GetAllUsedTransmissions() {
            
            ObservableCollection<LuaFile> listTransmission = new ObservableCollection<LuaFile>();
            listTransmission.Add(new LuaFile("Exists", true, "IVT", "Exampletext"));
            transmissionListView.ItemsSource = listTransmission;
            return listTransmission;
        }

        /// <summary>
        /// Create the default subfolders
        /// </summary>
        private void CreateSubFolders() {
            Directory.CreateDirectory("VCA");
            Directory.CreateDirectory("tmp");
        }


        /// <summary>
        /// Cleaning the folder VCA, when maybe was a previous run
        /// </summary>
        /// <param name="FolderName"></param>
        private void ClearFolder(string FolderName) {
            DirectoryInfo dir = new DirectoryInfo(FolderName);

            foreach (FileInfo fi in dir.GetFiles()) {
                fi.Delete();
            }

            foreach (DirectoryInfo di in dir.GetDirectories()) {
                ClearFolder(di.FullName);
                di.Delete();
            }
        }
}

C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vca_config_tool {
    public class LuaFile : IEnumerable<LuaFile> {

        private string transmission;
        private string transmissionName;
        private bool exists;
        private List<LuaFile> luaFiles;
        private String todoAction;
        private static List<string> actionList;

        public LuaFile(String action, bool existing, string NameOfTransmission, string transmissionContent) {
            todoAction = action;
            exists = existing;
            transmissionName = NameOfTransmission;
            transmission = transmissionContent;
            actionList = new List<string>();
            actionList.Add("Exists");
            actionList.Add("-");
            actionList.Add("Add");
            actionList.Add("Remove");
        }

        public LuaFile(bool existing, string nameOfFile) {
            exists = existing;
            transmissionName = nameOfFile;
        }

        public string TransmissionName {
            get => transmissionName;
            set => transmissionName = value;
        }
        public string Transmission {
            get => transmission;
            set => transmission = value;
        }

        public bool Exists { get; set; }

        public String TodoAction { get; set; }

        public IEnumerator<LuaFile> GetEnumerator() {
            return luaFiles.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator() {
            throw new Exception();
        }

        public IEnumerable<string> ActionList => actionList;
    }
}

XML:
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:vca_config_tool"
        xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:Properties="clr-namespace:vca_config_tool.Properties" x:Class="vca_config_tool.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="900"
        WindowStartupLocation="CenterScreen"
        Background="Transparent"
        >
    <Border
        Background="#3B28CC"
        CornerRadius="20"
        >
        <Grid>
            <Button x:Name="SelectZipBtn" Width="80" Height="20" HorizontalAlignment="Left" Margin="20,10,0,380" Click="SelectZipButtonClick">Select Zip</Button>
            <Button x:Name="DownloadBtn" Width="80" Height="20" HorizontalAlignment="Left" Margin="120,10,0,380" Click="DownloadButtonClick">
                <Button.IsEnabled>
                    <System:Boolean>false</System:Boolean>
                </Button.IsEnabled> Download
            </Button>
            <Button x:Name="ZipFileBtn" Width="110" Height="20" HorizontalAlignment="Left" Margin="220,10,0,380">
                <Button.IsEnabled>
                    <System:Boolean>false</System:Boolean>
                </Button.IsEnabled> Add and Zip File
            </Button>
            <ListView x:Name="transmissionListView" HorizontalAlignment="Left" Height="300" Width="400" Margin="20,58,0,0" VerticalAlignment="Top">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Transmission Name" Width="Auto" DisplayMemberBinding="{Binding TransmissionName}">
                        </GridViewColumn>
                        <GridViewColumn Header="Action" Width="100">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox Width="90" ItemsSource="{Binding ActionList}" SelectedItem="{Binding Path=TodoAction,Mode=TwoWay}"></ComboBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>


    </Border>
</Window>
 
Code:
listTransmission.CollectionChanged += (sender, e) =>
{
   // Wenn ein neues Item erstellt wurde, dann...
    if (e.Action == NotifyCollectionChangedAction.Add)
    {
        // Default Wert zuweisen
    }
};
 
Departet schrieb:
Code:
listTransmission.CollectionChanged += (sender, e) =>
{
   // Wenn ein neues Item erstellt wurde, dann...
    if (e.Action == NotifyCollectionChangedAction.Add)
    {
        // Default Wert zuweisen
    }
};
Sorry deine Lösung verstehe ich nicht ganz.
Wie wird damit der Wert in der Combobox gewählt? Ich verstehe dein code soweit das ich auf ein neues Element reagiere und nicht den Wert den ich in der Combobox auswählen möchte?
 
  • Gefällt mir
Reaktionen: Mystery1988
umask007 schrieb:
Würde der Auswahlbox noch einen Auswahlwert für nichts, z.B. "Select...",
hinzufügen, welches auch der Standardwert wäre.
Der Anwender kann nur aus den vorhandene Einträgen auswählen.
Ich möchte halt gerne, wenn ich ein neues Element erstelle den Wert aus der Dropdown Liste auswählen.

Ich schaue mir mal dein Link mal an
 
Zurück
Oben