DataBinding in CellEditDataTemplate not working when use ElementName - wpf

I have tried to update ItemSource of RadComboBox using ElementName property in Binding but when I use RelativeSource it is working as expected.
What is the difference.Can anyone please tell me.
<telerik:RadGridView Name="DesiredCapabilitiesGrid" RowIndicatorVisibility="Collapsed" AllowDrop="False" CanUserDeleteRows="True ShowGroupPanel="False" Height="Auto" Width="Auto"ItemsSource="{Binding DesiredCapabilities,Mode=TwoWay}"
GroupRenderMode="Flat" NewRowPosition="Bottom" Loaded="DesiredCapabilitiesGrid_Loaded">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Property Names" Width="2*" IsSortable="True" DataMemberBinding="{Binding DesiredCapabilityName}">
<telerik:GridViewDataColumn.CellEditTemplate>
<DataTemplate>
<telerik:RadComboBox ItemsSource="{Binding Path=DataContext.ConsiderDesiredCapabilites,ElementName="DesiredCapabilitiesGrid"}" IsEditable="True"></telerik:RadComboBox>
</DataTemplate>
</telerik:GridViewDataColumn.CellEditTemplate>
</telerik:RadGridView.Columns>
</telerik:RadGridView>

There is no element named DesiredCapabilitiesGrid in the same namescope as the RadComboBox in the CellEditTemplate. That's why the binding fails.
The RadComboBox is added to the visual tree and has visual ancestors though. That's why setting a RelativeSource work. Please refer to the link for more information about namescopes.

Related

Binding across telerik radgridview

I have a very tricky situation where I am binding data to telerik radgridview where everything works until I try and bind the visibility of one checkbox column based on the the check value of another of the columns. I have tried the following:
<telerik:RadGridView ShowGroupPanel="False" AutoGenerateColumns="False" ItemsSource="{Binding ParametersFilterConfigurations}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn IsFilterable="False" IsGroupable="False" IsSortable="False" DataMemberBinding="{Binding FilterElementName}" Header="Parameter Name" />
<telerik:GridViewCheckBoxColumn IsFilterable="False" IsGroupable="False" IsSortable="False" DataMemberBinding="{Binding CanView}" Header="Viewable"/>
<telerik:GridViewCheckBoxColumn IsVisible="{Binding CanView}" IsFilterable="False" IsGroupable="False" IsSortable="False" DataMemberBinding="{Binding CanFilterBy}" Header="Filterable" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
When I run this WPF attempts to locate the IsVisible="{Binding CanView" of the last column from the main datacontext and offcourse fails with the following:
System.Windows.Data Error: 40 : BindingExpression path error: 'CanView' property not found on 'object' ''MainWindow' (Name='')'. BindingExpression:Path=CanView; DataItem='MainWindow' (Name=''); target element is 'GridViewCheckBoxColumn' (HashCode=53813616); target property is 'IsVisible' (type 'Boolean')
How can I solve this? Please help.
I suggest using a cell template for the "Filterable" column. Since the "CanView" property changes per row, there's no way you can bind another entire column's visibility to that. What you can do is always show the column, but show/hide a checkbox in that column as needed. Ie, something like this:
<telerik:GridViewColumn>
<telerik:GridViewColumn.CellTemplate Header="Filterable">
<DataTemplate>
<CheckBox Visibility="{Binding CanView,Converter={StaticResource BoolToVisConverter}"
IsChecked="{Binding CanFilterBy}"
/>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>

Silverlight Template databinding issue

I am using the Telerik framework for creating a RadGridView with child elements in Silverlight 4.
<telerikGrid:RadGridView x:Name="itemsGrid" Grid.Row="1" AutoGenerateColumns="{Binding AutoGenerateColumn, Mode=TwoWay}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemsSource="{Binding Items, Mode=TwoWay}" CanUserInsertRows="True" ShowInsertRow="True" CanUserDeleteRows="True">
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewToggleRowDetailsColumn />
<telerikGrid:GridViewDataColumn Header="Item 1" DataMemberBinding="{Binding Item1}" UniqueName="Item1" />
<telerikGrid:GridViewComboBoxColumn Header="Group" UniqueName="Group"
ItemsSource="{Binding Groups.Items}" SelectedValueMemberPath="GroupId" DisplayMemberPath="GroupNames" />
<telerikGrid:GridViewComboBoxColumn Header="Supplier" UniqueName="Supplier"
ItemsSource="{Binding Suppliers}" SelectedValueMemberPath="CompanyS" DisplayMemberPath="CompanyName" />
<telerikGrid:GridViewDataColumn Header="Not in use" DataMemberBinding="{Binding inUse}" UniqueName="inUse" />
</telerikGrid:RadGridView.Columns>
<telerikGrid:RadGridView.ChildTableDefinitions>
<telerikGrid:GridViewTableDefinition />
</telerikGrid:RadGridView.ChildTableDefinitions>
<telerikGrid:RadGridView.HierarchyChildTemplate>
<DataTemplate>
<StackPanel>
<telerikGrid:RadGridView x:Name="childGrild" Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding ChildList}" CanUserInsertRows="True" CanUserDeleteRows="True" IsReadOnly="False" ShowInsertRow="True" ShowGroupPanel="False">
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewDataColumn Header="Part S" DataMemberBinding="{Binding PartS}" UniqueName="PartS" />
<telerikGrid:GridViewComboBoxColumn Header="Tools" ItemsSource="{Binding ElementName=control, Path=DataContext.Tools}" DataMemberBinding="{Binding PartS}" SelectedValueMemberPath="Id" DisplayMemberPath="Name" />
</telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>
</StackPanel>
</DataTemplate>
</telerikGrid:RadGridView.HierarchyChildTemplate>
</telerikGrid:RadGridView>
As you can see, each row in the grid has a child GridView, the problem is however that i cant get the Combobx in the gridview to display any data, its always empty. If i move it outside the child tempate it works fine, and the Items which are in the Tools list is shown.
To access the list, which is a property of the controls datacontext, i gave the control a Name (control), and used Path=DataContex.Tools, which i read should work.
Anyone has any idea why the ComboBox is not populated with data? I have checked that all the propertie names are correct, and that there actually are data there to show.
RadGridView and ElementName don't mix very well. In your case, the 'control' element can't be found. You could use a DataContextProxy as shown in the link. It solved an issue I had that was similar.

WPF Databinding issue

I'm using a RadGridView (Telerik). I'm trying to make a column editable only for certain rows, but what I'm trying doesn't seem to be working and I'm not sure how to do it.
My grid is this:
<telerik:RadGridView HorizontalAlignment="Left" Margin="12,12,12,12" Name="radGridView1" VerticalAlignment="Top" ItemsSource="{Binding FeedList}" ShowGroupPanel="False" AutoGenerateColumns="False" ShowInsertRow="True" EditTriggers="Default">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Feed" DataMemberBinding="{Binding Path=Name}" Width="150" IsReadOnly="{Binding Path=ReadOnly}" />
<telerik:GridViewDataColumn Header="Url" DataMemberBinding="{Binding Path=Url}" Width="*"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
The bound object has a property, but the Path for the IsReadOnly binding is relative to the DataContext for the form and not the bound object. How would I make it relative to the bound object in this case?
In case anyone comes across this on a search. The answer is the GridView's datacontext is bound to the collection it's displaying and doesn't have access to the Window or UserControl's datacontext.
Thomas Levesque has a blog entry on the subject. He puts together a BindingProxy class that you can use to pass in the datacontext to the radgridview.
http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
Intellisense must have been messed up. Somehow missed the IsReadOnlyBinding property of GridViewDataColumn, which does exactly what I want. Not sure why IsReadOnly couldn't do it, though.

Element Data-Binding in Silverlight

I have a template column in a DataGrid:
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<TextBlock Text="{Binding Name,ElementName=rsAllSkills}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
And in the same xaml file, I have
<riaControls:DomainDataSource QueryName="GetSkillsQuery" AutoLoad="True" x:Name="rsAllSkills">
<riaControls:DomainDataSource.DomainContext>
<domain:XXXX context/>
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
The DataSource has loaded everything successfully for sure, if I put that TextBlock out side of the DataGrid, it works; but inside the DataGrid, it doesn't load even the Name of rsAllSkills....
Could anybody give me a hint, thank you so much.
Have a dummy converter and check the binding.
What I guess is, the DataTemplate inside the CellEditingTemplate would receive the parent's DataContext, ie., DataGrid's DataContext. So, to work around this you can do one thing.
1) Bind the rsAllSkills to the Tag Property of DataGridTemplateColumn.
2) Now, Bind the TextBlock's Text property with the Tag property like,
<sdk:DataGridTemplateColumn Tag="{Binding Name,ElementName=rsAllSkills}">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<TextBlock Text="{Binding Tag}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>

How can I achieve a binding path which is bound by another binding?

I'm writing my own UserControl which displays data in a ListBox. I want to achieve something similar to a property like "DisplayMemberPath". In my example, it's "EditableMemberPath", which should determine which member will be displayed in the textbox. However, using a binding on a binding does not work.
<ListBox ItemsSource="{Binding Path=Items, ElementName=me}"
SelectedItem="{Binding Path=SelectedItem, ElementName=me}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Converter={Binding Path=ItemToImageConverter, ElementName=me}}" />
<TextBlock Text="{Binding Path={Binding Path=EditableMemberPath, ElementName=me}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Can you help me finding a proper solution?
Best Regards
Oliver Hanappi
You can't do that directly in XAML (at least not with the built-in classes) : a Binding is not a DependencyObject, so you can't bind its properties.
However the post mentioned by Martin Harris looks promising...

Resources