mardi 7 août 2018

WPF Opening a file and updating window

I am relatively new to WPF. I am trying to open an excel file and pull the column headers and displaying it in my window as a check list. Right now I'm having issues updating the my window/checklist.

Here is what I have in the xaml

    <DockPanel Grid.Column="0" Grid.Row="1" Margin="10">
        <ListBox ItemsSource="{Binding TagListData}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsTagSelected}" Content="{Binding TagName}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DockPanel>

and here is what I have in the cs code. (Instead of reading the excel document that gets opened, I am just using a placeholder for now just to see if I'm doing this correctly.)

    private Excel.Application xlApp;
    private Excel.Workbook xlWorkbook;
    public ObservableCollection<TagClass> TagListData { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        ...

        TagListData = new ObservableCollection<TagClass>();
    }

    private void btnOpenFile_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.slxm";
        if (openFileDialog.ShowDialog() == true)
        {
            xlApp = new Excel.Application();
            xlWorkbook = xlApp.Workbooks.Open(openFileDialog.FileName);

            //populate TagListData
            TagListData.Add(new TagClass { IsTagSelected = true, TagName = "Tag Name 1" });

        }
    }

    public class TagClass
    {
        public string TagName { get; set; }
        public bool IsTagSelected { get; set; }

    }    

When I try top open a file to populate my checklist, nothing happens. Does anyone know what I am doing incorrectly?

I also found this which checks when an item gets updated but I want to check when the list/collection gets updated. I'm having a hard time figuring this out.. ListBox item doesn't get refresh in WPF?

Thank you




Aucun commentaire:

Enregistrer un commentaire