Hi,
We are using XAMGrid and we are going to bind header Text dynamically. Our requirement is that As we select the row as per row selection the header text will be changed. For that we make bind Header text in ViewModel. But Header Text are not showing in UI View.
Please help us for updating Header Text in XAMGrid.
Thanks
Hi piyu,
Bindings won't work on the HeaderText property because the column object (i.e. TextColumn, ComboBoxColumn, etc) does not exist within the visual tree. You will also notice that these columns do not have a DataContext property. So binding to anything other than a StaticResource will not work.
There are a two ways to can get around this. The first is to declare your view model as a StaticResource in the XAML page and then change all your bindings to be "{Binding Source={StaticResource myVM} Path=myproperty}". The second option is to bind the column HeaderText property to your view model property in code-behind:
Binding b = new Binding("ViewModelHeaderProperty");b.Source = vm;b.Mode = BindingMode.TwoWay;BindingOperations.SetBinding(column, ColumnBase.HeaderTextProperty, b);
Thanks for your Reply. I have applied First approach but Header text Property are not going to visible in XAMGrid.
In XAML defined Viewmodel in Key:
<local:GlobalHistoryRecordInfoVM x:Key="globalHistoryKey"/> <ig:XamGrid x:Name="dataGlobalHistoryView" ItemsSource="{Binding Path=globalHistoryRecordInfo}" AutoGenerateColumns="False" " SelectedRowsCollectionChanged="dataGlobalHistoryView_SelectedRowsCollectionChanged"> <ig:XamGrid.EditingSettings> <ig:EditingSettings AllowEditing="None" /> </ig:XamGrid.EditingSettings> <ig:XamGrid.SelectionSettings> <ig:SelectionSettings CellClickAction="SelectRow" RowSelection="Single" /> </ig:XamGrid.SelectionSettings> <ig:XamGrid.RowSelectorSettings> <ig:RowSelectorSettings Visibility="Hidden"/> </ig:XamGrid.RowSelectorSettings> <ig:XamGrid.Columns>
<ig:TextColumn Key="Param2" Width="Auto" > <ig:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Source={StaticResource globalHistoryKey}, Path= ModelHistory .ParamHeader2}" Name="txtBlockHeader2" FontSize="12" /> </DataTemplate> </ig:TextColumn.HeaderTemplate> </ig:TextColumn>
</ig:XamGrid.Columns> </ig:XamGrid>
Now in ViewModel I have assigned ColumnHeader Text..
public GlobalHistoryRecordInfoVM() { ModelHistory = new GlobalHistoryModel(); ModelHistory.ParamHeader2 = "Param2"; }
Above I have assigned Param2 as Header Text. But Param2 is not going to visible in Grid Header..
Please let us aware..
I added your XAML to a test project and as long as the view model objects existed the binding worked and I can see the "Param2" text in the column header. I'm not sure why it doesn't work for you. Compare your setup to my sample and see what is different.
Is there a way to accomplish this from XAML accessing a property from viewmodel, I do have a dynamic grid that will have different column header value based on a property, so the static resource will not suffice for me, I can't get into the binding from column header so is there a solution for that situation?
Thanks!
SM
Hello,
The Columns(i.e. TextColumn, ComboBoxColumn, etc) in XamGrid are not visual elements(not exist within the visual tree) and since they don't derive from the FrameworkElement class they don't expose DataContext property. In order to bind the Columns's HeaderText to property from your view model, you will need to use the model as a Source or static resource in the Binding. You can download 'BindingColumnHeaderText.zip' project from Rob's post and test this approach.
Also you can read the 'Binding to Properties of non-FrameworkElements' blog post from our web site, for additional details about 'Binding to StaticResource' and 'Binding to a Data Context Proxy' approaches.
Let me know if you have any questions.
Sincerely,ZhivkoAssociate Software Developer
Unfortunately loading value from the resource file or static resource will not work as the view is dynamic and created via data template.
I looked into this and I don't want to instantiate the view model again with the default constructor as the property already has the value I'm looking for..