3

Is there a way to xamly set a StaticResource per row in a DataGrid accessing it from all the columns?


UPDATE
My aim is to have 3 ComboBox columns, while only the last one is actually bound to the rows item.
For instance, the DataGrid represents a list of Items. I have Category->Vendor->Style->Finish ComboBoxes, but those are only for navigation assistance, in fact, the Item class has only a 'Finish' relationship. So if there coulda been a StaticResource per row, I could set the ItemsSource & IsSynchronizedWithCurrentItem props of the ComboBox and this would work automatically.

Thanks a lot.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

4 Answers4

1

Technically, I guess you could because the row is in the visual tree. But what are you trying to achieve? There's probably a better way.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • I wanted to do 2 cascading ComboBoxes. I put them both on 1 DataGridTemplateColumn setting the CollectionViewSource in the DataTemplate.Resources. – Shimmy Weitzhandler Apr 22 '10 at 05:39
  • But it would still be a great idea to have a resource for each row, because now I have all the cascading comboboxes in one column, if i would be able to have a resource per row, i could separate them between columns. Waiting to hear forward. – Shimmy Weitzhandler Apr 23 '10 at 04:24
0

Can you elaborate a little bit more on what you are trying to achieve ?

I'd think it is simpler to have one staticResource which represents a Collection (See ObjectDataProvider type) and then bind the DataGrid's ItemSource property to it in XAML.

Gishu
  • 134,492
  • 47
  • 225
  • 308
  • I want to treat each row with its custom DataSource, take a look at what I did, it's on Josh's response. – Shimmy Weitzhandler Apr 22 '10 at 09:45
  • But it would still be a great idea to have a resource for each row, because now I have all the cascading comboboxes in one column, if i would be able to have a resource per row, i could separate them between columns. Waiting to hear forward. – Shimmy Weitzhandler Apr 23 '10 at 04:25
  • @Shimmy : it looks like you're using the wrong control here for the job. Are you looking for a series of comboboxes where the choice in one filters the options for the next combobox in sequence ? – Gishu Apr 24 '10 at 03:41
  • I have a DataGrid that shows 'Product' entities. I included these cols: Vendor, Style, Finish; while the chain works this way: Product.Finish.Style.Vendor. So I want to make in each of these columns a combobox that cascades according to the previous selected value in the prev column. – Shimmy Weitzhandler Apr 25 '10 at 08:55
  • @Shimmy - So if I design the Product class to expose Properties called Finish, Style and Vendor. Bind each column of the datagrid to the appropriate property. The properties themselves lookup the deciding sibling property and return the right list for the dropdown in the combobox e.g. Style would see lookup the value of this.Finish in an internal hashtable to get the list of allowed choices for the Style value. Would that work for you ? – Gishu Apr 26 '10 at 05:46
  • Please take a look at: http://stackoverflow.com/questions/3203416/accessing-control-between-datagridcells-dynamic-cascading-comboboxes – Shimmy Weitzhandler Jul 10 '10 at 21:48
0

You can certainly set it at the DataGrid level like this:

xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

<data:DataGrid x:Name="..."  ItemsSource="{Binding ...}" >
    <data:DataGrid.Resources>
    </data:DataGrid.Resources>
</data:DataGrid>

I would presume you can set it at row level if you define a row template?

slugster
  • 49,403
  • 14
  • 95
  • 145
  • No, I want it to be set per row, not per the Control container. My aim is to set the CollectionViewSource within the row according to a value the user selected for this row. – Shimmy Weitzhandler Jul 10 '10 at 21:48
0

What I did and solved my issue (no guarantees for whether this is the proper way), I am using a UserControl as the DataTemplate content, having the resource declared in the UserControl, then it gets initialized each time.

Another thing I havn't tried is setting x:Shared attribute to false, which I believe is supposed to help with the issue.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632