none
Is it possible to set a custom class object (the whole object, not a member only) to a bindable attribute? RRS feed

  • Question

  • Hello,

    I would like to know if its possible to set a custom class object (the whole object, not a member only) to bindable attribute.

    As you can see from the code snippet, I have a collection of BackupEntry object.

    BackupEntry is a class with primitive members, such as string ID, bool IsChecked, string Filename, etc..

    I assigned the collection to a listview as item source. The bindable attribute I was talking about before is the TAG attribute in this case.

    So basically, my purpose is to assign the TAG the BackupEntry object (which is actually displayed in that row as well).

     

    If I had that BackupEntry object in TAG attribute, then in code behind, I could recreate that object and get all the data from that particular row.

    The SelectedItem(s) properties are not usable for me, since my selection is based on the check boxes.

    I don't want to use the IsChecked member variable since that would involve me having to ITERATE through the collection just having to check its boolean variable. What I have now (if I swap BackupEntry with ID) are just the IDs but I also need other info from that object.

     

    Please tell me if this is possible, and if not, alternatives which hopefully will not be dramatically different from what I currently have.

     <ListView Margin="16,159,16,72"
             Foreground="Black"
             Name="gridFiles"
             ItemsSource="{Binding Path=BackupEntryCollection}">
          <ListView.View>
            <GridView AllowsColumnReorder="true"
                 ColumnHeaderToolTip="Authors">
              <GridViewColumn>
                <GridViewColumn.CellTemplate>
                  <DataTemplate>
                     <CheckBox Name="chkRow"
                          IsThreeState="False"
                          Tag="{Binding BackupEntry}"
                          IsChecked="{Binding Path=IsChecked}"
                          Checked="chkRow_Checked"
                          Unchecked="chkRow_Checked" />
                   ....
    
    Tuesday, May 24, 2011 10:30 AM

Answers

  • Hi Mark Smith X,

    If you want to bind to the Whole Object, you could just set the binding like:

    Tag="{Binding Path=., Converter={StaticResource TestConvert}}"

    the main point is Path = ., that converter is just for test.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 27, 2011 6:10 AM
    Moderator
  • In case anybody is interested, here is the other way:

    private void chkRow_Checked(object sender, RoutedEventArgs e)
            {          
                CheckBox chk = (CheckBox)sender;
                BackupEntry item = (BackupEntry )chk.DataContext;

                MessageBox.Show("Checked/Unchecked: " + item.ID);
            }

     

    No need for the TAG binding. Simpler!

    • Marked as answer by Mark Smith X Tuesday, June 7, 2011 8:29 AM
    Tuesday, June 7, 2011 8:29 AM

All replies

  • Hi Mark Smith X,

    If you want to bind to the Whole Object, you could just set the binding like:

    Tag="{Binding Path=., Converter={StaticResource TestConvert}}"

    the main point is Path = ., that converter is just for test.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 27, 2011 6:10 AM
    Moderator
  • Hi Mark Smith X,

    I am marking your issue as "Answered", if you have new findings about your issue, please let me know.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 1, 2011 9:47 AM
    Moderator
  • Thanks for your reply!

    I actually had to use another method but if I ever need this again (or need to revert), I will definitely use this.. thanks!

    Tuesday, June 7, 2011 2:43 AM
  • In case anybody is interested, here is the other way:

    private void chkRow_Checked(object sender, RoutedEventArgs e)
            {          
                CheckBox chk = (CheckBox)sender;
                BackupEntry item = (BackupEntry )chk.DataContext;

                MessageBox.Show("Checked/Unchecked: " + item.ID);
            }

     

    No need for the TAG binding. Simpler!

    • Marked as answer by Mark Smith X Tuesday, June 7, 2011 8:29 AM
    Tuesday, June 7, 2011 8:29 AM