Matt davis
Matt davis

Reputation: 93

Two way binding on a DataGrid

I am trying to create a simple program that will allow the user to Select files and add metadata to it.

I have created a ListBox that allows users to drag and drop files onto it. I grab the path and save it off into an object. I then display the file name in the ListBox.

I want it so that when a User selects an item in the list box I can show the metadata I have on the file and allow them to add more and edit what is there.

Right now I have a Class Item that stores the Path and a dictionary of <string, string> Where the Key is the name of the metadata and the Value is well the value.

I have tried using a DataGrid, and maybe that is the wrong control to use, to bind to the Dictionary. That doesn't seem to be the right way to do it as it doesn't implement the INotifyPropertyChanged Interface.

I could create my own class and manually update the DataGrid but it seems like that is a work around to me not knowing how to DataBind correctly.

XAML

<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"
    mc:Ignorable="d"
    x:Class="MetadataAdder.MainWindow"
    Title="Metadata Adder" Height="480" Width="640">
<Grid>
    <Button x:Name="Add_BTN" Content="Add" HorizontalAlignment="Left" Margin="10,410,0,0" VerticalAlignment="Top" Width="50" Click="Add_Click"/>
    <Button x:Name="Remove_BTN" Content="Remove" HorizontalAlignment="Left" Margin="241,410,0,0" VerticalAlignment="Top" Width="50" Click="Remove_Click"/>
    <ListBox x:Name="File_List" HorizontalAlignment="Left" Height="364" Margin="10,31,0,0" VerticalAlignment="Top" Width="281" AllowDrop="True" Drop="FilesDropped" ItemsSource="{Binding Item_List}" SelectionChanged="Item_Selected"/>
    <DataGrid 
        x:Name="MetadataGrid" 
        HorizontalAlignment="Left" 
        Margin="311,31,0,0" 
        VerticalAlignment="Top" 
        Height="364" 
        Width="303" 
        d:LayoutOverrides="GridBox"
        CanUserAddRows="False"
        CanUserDeleteRows="False"
        CanUserReorderColumns="True"
        CanUserResizeColumns="True"
        CanUserResizeRows="True"
        CanUserSortColumns="True"
        />
    <Label Content="Files to add Metadata to" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"/>
    <Label Content="Metadata" HorizontalAlignment="Left" Margin="313,5,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.474,0.115"/>

</Grid>

Upvotes: 2

Views: 159

Answers (1)

KodeKreachor
KodeKreachor

Reputation: 8882

Another approach that would work well for you would be to create a FileMetadata object of your own that implements INotifiyPropertyChanged and contains properties for the metadata's Key and Value.

Then storing your collection of FileMetadata objects in an ObservableCollection and binding to your DataGrid.

This would allow the individual metadata items to persist their values to the change notification system as well as allow the DataGrid to automatically update if any metadata items are added or removed.

Upvotes: 1

Related Questions