I am binding a data grid to a collection of Task objects. A particular column needs some special rules pertaining to editing:
<!--Percent Complete-->
<data:DataGridTextColumn Header="%"
ElementStyle="{StaticResource RightAlignStyle}"
Binding="{Binding PercentComplete, Mode=TwoWay, Converter={StaticResource PercentConverter}}" />
What I want to do is set the IsReadOnly property only for each task's percent complete cell based on a property on the actual Task object. I've tried this:
<!--Percent Complete-->
<data:DataGridTextColumn Header="%"
ElementStyle="{StaticResource RightAlignStyle}"
Binding="{Binding PercentComplete, Mode=TwoWay, Converter={StaticResource PercentConverter}}"
IsReadOnly={Binding IsNotLocalID} />
but apparently you can't bind to the IsReadOnly property on a data grid column. What is the best way do to do what I am trying to do?
I don't think that you can Bind directly to this. I have found this extended DataGrid for Silverlight which will do the trick though.
Extended DataGrid
It looks like the DataGridColumn.IsReadOnly Property is a DependencyProperty so it should be bindable. Change your XAML to IsReadOnly="{Binding IsNotLocalID}" (Note the added quotes) and see what happens. Are you getting any binding failures in the Visual Studio output window?
Related
I have an xceed:DataGridControl with bounded ItemsSource. Currently I'm trying to set my in/visible columns and the title/headertext for each visible column. Preferably I would like to bind a property in my ViewModel, to set the in/visible columns and theirs titles. But I find no way I could do it. Does anyone know a solution to this problem?
<xceed:DataGridControl
x:Name="dataGridControl"
SelectedItem="{Binding SelectedTextItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ItemsSource="{Binding ItemsSourceData, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
</xceed:DataGridControl>
Yes indeed I had to deal with xceed's controls few months ago.
DataGridControl allows you to generate columns automantically. That is also its default behavior.
In order to have your own columns you will have to disable the property AutoCreateColumns and futhermore you will have to set few columns on the property DataGridControl.Columns.
There you will be able to bind Visible property of the Column.
Thanks to Peter for providing this code:
<xceed:DataGridControl ItemsSource="{Binding TextSet}" >
<xceed:DataGridControl.Columns>
<xceed:Column FieldName="ColumnId" Title="{Binding DatagridTitle[ColumnId], Mode=OneWay}" Visible="True" />
</xceed:DataGridControl.Columns>
</xceed:DataGridControl>
I also met similar issue.
You can use the Visible property, then do following:
<xcdg:ColumnFieldName="ColumnId" Title="ColumnId"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type typeOfAncestor}}, Path=DataGridControl.DataContext.BooleanSourceProperty}"/>
For example, if the typeOfAncestor is xcdg:MergedColumn and BooleanSourceProperty is IsVisble, then the code should be:
<xcdg:ColumnFieldName="ColumnId" Title="ColumnId"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type xcdg:MergedColumn}}, Path=DataGridControl.DataContext.IsVisible}"/>
Then the issue can be solved.
I have a grid column defined. The parent grid gets its items from an ObservableCollection of type ItemClass. ItemClass has two properties: String Foo, and bool IsEditAllowed.
This column is bound to property Foo. There's a control template for editing the cell. I'd like to bind the ItemClass.IsEditAllowed property to the IsEnabled property of the TextBox in the template.
The question is how to bind it. Can this be done? The XAML below gets me "Cannot find source for binding with reference" in the debug trace.
The grid will let me bind the ItemClass itself to the field via some "custom" event thingy, and I can then bind to any of its properties. That's fine, but it seems kludgy. But if it's the only way, it's the only way.
<dxg:GridColumn
Header="Foo Column"
FieldName="Foo">
<dxg:GridColumn.EditTemplate>
<ControlTemplate>
<TextBox Text="{Binding Value, Mode=TwoWay}"
IsEnabled="{Binding Path=IsEditAllowed, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ItemClass}, AncestorLevel=1}}" />
</ControlTemplate>
</dxg:GridColumn.EditTemplate>
</dxg:GridColumn>
There are two potentially easier ways to set up this binding.
Name the grid. Then your binding could look something like this (assuming dxg:GridControl has a property named "Items" and that you have assigned an instance of your ItemClass to that property):
<TextBox IsEnabled="{Binding Path=Items.IsEditAllowed, ElementName=MyGridControl} />
Use relative binding, but look for the GridControl rather than something nominally internal to the way GridControl works (that is, GridControlContentPresenter). This gets you away from the implementation details of GridControl, which are perhaps more likely to change in ways that break your application than are properties on GridControl itself.
<TextBox IsEnabled="{Binding Path=Items.IsEditAllowed, RelativeSource={RelativeSource AncestorType={x:Type dxg:GridControl}}}" />
You may also want to read up on the Visual Tree and the Logical Tree in WPF/xaml. The "Ancestor" in relative bindings refers to ancestors in the visual tree, that is, things like parent containers, and not to super- or base classes (as you've discovered, I think).
Here's the answer[1]. FindAncestor finds ancestors in the runtime XAML tree, not in arbitrary C# objects. It cannot walk up to the ItemClass instance from the member we're bound to. But we do know that somebody above us in the XAML tree bound us to that member, and he was bound to the ItemClass instance itself. So whoever that is, we find him, and then we've got the ItemClass.
So let's add debug tracing to the binding, and we'll see what the XAML situation looks like at runtime. No doubt there are other and probably smarter ways to do that, but I happen to know this one without any research.
First add this to the namespaces at the top of the XAML file:
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
...and then to the binding itself, add this:
diag:PresentationTraceSources.TraceLevel=High
Like so:
<TextBox Text="{Binding Value, Mode=TwoWay}"
IsEnabled="{Binding Path=IsEditAllowed, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ItemClass}, AncestorLevel=1}, diag:PresentationTraceSources.TraceLevel=High}"
/>
At runtime, when the TextEdit's IsEnabled property tries to get a value from the binding, the binding walks up through the XAML tree looking for an ancestor of the specified type. It keeps looking until it finds one or runs out of tree, and if we put tracing on it, it traces the type of everything it finds the whole way up. We've told it to look for garbage that it'll never find, so it will give us a trace of the type of every ancestor back to the root of the tree, leaf first and root last. I get 75 lines of ancestors in this case.
I did that, and found a few likely candidates. I checked each one, and the winner turned out to be dgx:GridCellContentPresenter, which has a RowData property. RowData has a lot of properties, and RowData.Row is the row's instance of ItemClass. dxg:GridCellContentPresenter belongs to the DevExpress grid library we're using; in another vendor's grid class, there would presumably be some equivalent.
Here's the working binding:
<TextBox Text="{Binding Value, Mode=TwoWay}"
IsEnabled="{Binding Path=RowData.Row.IsEditAllowed, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dxg:GridCellContentPresenter}, AncestorLevel=1}}"
/>
If DevExpress, the vendor, rewrites their GridControl class, we'll be in trouble. But that was true anyhow.
...
[1] Better answer, though it's too DevExpress specific to be of any real interest: The DataContext of the TextBox itself turns out to be dxg:EditGridCellData, which has a RowData property just like GridCellContentPresenter does. I can just use IsEnabled="{Binding Path=RowData.Row.IsEditAllowed}".
However, what I really wanted to do all along was not to present a grid full of stupid disabled textboxes, but rather to enable editing on certain rows in the grid. And the DevExpress grid lets you do that through the ShowingEditor event.
XAML:
<dxg:GridControl Name="grdItems">
<dxg:GridControl.View>
<dxg:TableView
NavigationStyle="Cell"
AllowEditing="True"
ShowingEditor="grdItems_TableView_ShowingEditor"
/>
</dxg:GridControl.View>
<!-- ... Much XAML ... -->
</dxg:GridControl Name="grdItems">
.cs:
private void grdItems_TableView_ShowingEditor(object sender, ShowingEditorEventArgs e)
{
e.Cancel = !(e.Row as ItemClass).IsEditAllowed;
}
I'm trying to work with a datagrid using the MVVM pattern. The problem is that whenever I change the VM property which is binded to SelectedItem to null, the View doesn't "deselect" the currently selected item. This is my binding in xaml:
<DataGrid Grid.Column="0" Grid.Row="0"
ItemsSource="{Binding Path=Users}"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
SelectedItem="{Binding Path=SelectedUser, Mode=TwoWay}">
The SelectedItem binding works from the view to the VM thus in the SelectedUser property I always have the selected object. The problem is that in the VM I'm doing some stuff which sometimes changes the SelectedUser property to null so I would expect the datagrid to deselect the row as well. Instead, it remains selected and if I try to click on the same row, the property doesn't update. If I click on any other row, the property changes as expected.
Is there a way to make the datagrid deselect if it's binded property is set to null? Also I'm looking for a MVVM solution as I don't want to write code behind. I can solve this by writing code behind so don't waste time offering such solutions :)
l.e.: this is my property in the VM:
public RPLUser SelectedUser
{
get
{
return selectedUser;
}
set
{
selectedUser = value;
OnPropertyChanged("SelectedUser");
}
}
Thanks in advance!
I recommend to check the Output Window in visual studio and see if any Binding is failing.
Are you sure when you select something, the selection updates into the SelectedUser property?
Did u put a breakpoint in setter of SelectedUser and see that it is hitting when you select something on the datagrid?
The reasons for this Binding to break could be many ...
SelectedUser is of different type than individual Users.
SelectedUser does not match by reference with any items in Users.
How and where are you setting null?
The following code in my case works perfectly fine...
<tk:DataGrid MaxHeight="200" AutoGenerateColumns="False"
ItemsSource="{Binding}"
SelectedItem="{Binding MySelItem,
ElementName=MyDGSampleWindow,
Mode=TwoWay}"
IsReadOnly="True">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Key"
Binding="{Binding Key,
Mode=OneWay}"/>
<tk:DataGridTextColumn Header="Value"
Binding="{Binding Value,
Mode=OneWay}"/>
</tk:DataGrid.Columns>
</tk:DataGrid>
When I set MyDGSampleWindow.MySelItem as null, the datagrid propertly deselects. Perhaps you might need to give us more input on how are you actually setting the value as null.
Did you try setting IsSynchronizedWithCurrentItem="True" in the xaml properties for the DataGrid? AFAIK, this will allow you to unselect it by setting the SelectedUser to null.
I cannot test it at the moment, but you could also try to add this in the setter of your property:
set
{
selectedUser = value;
OnPropertyChanged("SelectedUser");
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Users);
collectionView.MoveCurrentTo(selectedUser);
}
(For ICollectionView to do anything, you will need to have IsSynchronizedWithCurrentItem set)
Like I said, I cannot test this right now. Also, the setter of the property is probably not the best place to put it. Maybe create an event handler for the PropertyChangedevent locally and put that logic there.
Let me know if it helps, else I'll see if I can run a short test...
Yeah may need to add the XAML UpdateSourceTrigger to update the UI.
SelectedItem="{Binding SomeProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
The DataGrid will not Deselect it automatically as DataGridRow's IsSelected property should be set to False.
You can do that by Setting a style on DataGrid.. like
<Style x:Key="dataGridRowStyle"
BasedOn="{StaticResource {x:Type WPFToolkit:DataGridRow}}"
TargetType="{x:Type WPFToolkit:DataGridRow}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected}" />
</Style>
The IsSelected property should be the of the object i.e in your case RPLUser should have a property Isselected
Then before you set the SelectedUser to null... just do SelectedUser.IsSelected=False
And dont forget to attach this style to the DataGridRowStyle in Datagrid
I am using WPFToolkit you can modify the style if you are targetting .NET 4.0
I have a simple problem binding an entity to datagrid in wpf.
I have an entity Called "User".... and each "User" have one "Workgroup" .. the relationship between the two is one to one.
now in EF every User entity has one workgroup entity inside.
when I want to bind Users Collection to datagrid I have no clue hot to say that you have to put forexample workgroup.Title inside a datagrid Column
I'm trying to bind in this way :
XAML:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Users}" HorizontalAlignment="Stretch" Margin="5" Name="dgUserList" VerticalAlignment="Stretch" SelectionChanged="dgUserList_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName" />
<DataGridTextColumn Binding="{Binding LastName}" Header="LastName" />
<DataGridTextColumn Binding="{Binding Username}" Header="UserName" />
<DataGridTextColumn Binding="{Binding WorkGroup}" Header="Workgroup" />
</DataGrid.Columns>
</DataGrid>
Code Behind :
Created a property like this :
public List<User> Users
{
get { return dal.GetUsers(); }
}
and do the binding :
private void BindGrid()
{
dgUserList.ItemsSource = Users;
}
this work file with direct properties of User Entity but it puts type of Workgroup entity inside the datagrid column and the reason is obvious. I want to put the Title of workgroup inside
how can i achive this ?
any help would be greatly appreciated
WPF Bindings support nested properties so to get to any of the sub-properties on a property of a bound object just use normal "." syntax:
<DataGridTextColumn Binding="{Binding WorkGroup.Title}" Header="Workgroup" />
You also don't need to set the ItemsSource twice. If you have the DataContext of the DataGrid set up as the Window (or UserControl, etc.) whose code-behind declared the Users property then the ItemsSource Binding in XAML is sufficient and you can remove the BindGrid method. If you haven't set the DataContext, the XAML ItemsSource Binding isn't doing anything (you can probably see an error message in your Debug Output) so you can remove that and just let the code-behind method take care of it.
You should also consider using an ObservableCollection to get automatic notification and UI updates when items are added or removed. Since you're already using EF you may also be able to just use the EntityCollection for User, which includes the same automatic INotifyCollectionChanged notification.
I am creating a user control to display a three-month calendar. The control is based on the WPF Calendar control (WPF Toolkit 2009-06), and I want to pass several of the Calendar's properties through to corresponding properties of my user control. The user control properties are set up as Dependency Properties, and their underlying types match the types of the Calendar properties. Here is my markup:
<StackPanel>
<toolkit:Calendar Name="MasterCalendar"
SelectionMode="{Binding Path=SelectionMode, Mode=OneWay}"
SelectedDate="{Binding Path=SelectedDate, Mode=OneWayToSource}"
SelectedDates="{Binding Path=SelectedDates, Mode=OneWayToSource}"/>
<toolkit:Calendar Name="SlaveCalendar1"
DisplayDate="{Binding DisplayDate, Converter={StaticResource IncrementalMonthConverter}, ElementName=MasterCalendar, Mode=OneWay}"
SelectionMode="{Binding Path=SelectionMode, Mode=OneWay}"
SelectedDate="{Binding Path=SelectedDate, Mode=OneWayToSource}"
SelectedDates="{Binding Path=SelectedDates, Mode=OneWayToSource}"/>
<toolkit:Calendar Name="SlaveCalendar2"
DisplayDate="{Binding DisplayDate, Converter={StaticResource IncrementalMonthConverter}, ElementName=SlaveCalendar1, Mode=OneWay}"
SelectionMode="{Binding Path=SelectionMode, Mode=OneWay}"
SelectedDate="{Binding Path=SelectedDate, Mode=OneWayToSource}"
SelectedDates="{Binding Path=SelectedDates, Mode=OneWayToSource}"/>
</StackPanel>
All of the properties bind without problem, except for the SelectedDates property. I get the following error on its binding:
'SelectedDates' property is read-only and cannot be set from markup.
I suspect that it is because the SelectedDates property is a collection, but I am not sure how to fix the problem. Can anyone enlighten me on the cause of the problem and suggest a fix? Thanks for your help.
If I understand you well, you have Dependency properties in your code behind that match in name and type the properties of the Calendar Controls in your user control. You are trying to assign the SelectedDates Collection of the various Calendar Controls to the Dependency property of the same name in your code behind.
You can simply do this by a line of code:
this.SelectedDates=SlaveCalendar1.SelectedDates
In an appropriate EventHandler that fires when a selected date is added.
Even though you set the binding to OneWayToSource the SelectedDates= piece of code is an assignment. As the SelectedDates Property has no setter, it is not possible to write this piece of code.
Here you can find a link to the Calendar Control's documentation