I'm using the datagrid from the WPF Toolkit for 3.5.
I have a need to TextWrap one of my columns in this grid so have embedded a TextBlock in this column.
The issue I have now is that when I copy the grids content into excel (Ctrl A, Ctrl C) this column is blank.
Any help on resolving this would be much appreciated. Below is my XAML for the grid.
<tk:DataGrid Name="dgDataGrid" Margin="3" AutoGenerateColumns="False" HeadersVisibility="All"
ClipboardCopyMode="IncludeHeader" SnapsToDevicePixels="True"
CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True"
SelectionMode="Extended" SelectionUnit="Cell">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Binding="{Binding ID}" Header="Message ID" Visibility="Hidden" FontWeight="Black" />
<tk:DataGridTextColumn Binding="{Binding UserInitials}" Header="User" Width="Auto" />
<tk:DataGridTextColumn Binding="{Binding EntryDate}" Header="Time Stamp" Width="Auto" />
<tk:DataGridTemplateColumn Header="Message" Width="*">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="0" Text="{Binding Message}" TextWrapping="Wrap"/>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
</tk:DataGrid.Columns>
</tk:DataGrid>
Set the ClipboardContentBinding property on the DataGridTemplateColumn.
Related
I have a datagrid like below
**strong text**
<sdk:DataGrid AutoGenerateColumns="False" Height="Auto" MaxHeight="500"
HorizontalAlignment="Left" Margin="5" Name="_EmployeeGrid"
ItemsSource="{Binding Path= Employees,Mode=TwoWay}"
HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Visibility="Visible"
VerticalAlignment="Top" Width="Auto">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Employee ID" IsReadOnly="True" Binding="{Binding Path=EmpID}"/>
<sdk:DataGridTemplateColumn Header="Name" SortMemberPath="EmpID">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton VerticalAlignment="Center" NavigateUri="{Binding Path=EmpID,
Converter={StaticResource NavigatePropertyUriConverter}, ConverterParameter=EmpView}"
Content= "{Binding Path=EmpID}" >
</HyperlinkButton>
<!--<HyperlinkButton NavigateUri= "{ Binding Converter={StaticResource navigateConv}, ConverterParameter=/Property/IssuesView?}" VerticalAlignment="Center"
Content="{Binding Path=PropertyId}"
HorizontalAlignment="Right" Margin="5"/>-->
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<!--<sdk:DataGridTextColumn Header="Property #" IsReadOnly="True" Binding="{Binding Path=PropertyId}"/>-->
<sdk:DataGridTextColumn Header="Address" IsReadOnly="True" Binding="{Binding Path=Address}"/>
<sdk:DataGridTextColumn Header="Category" IsReadOnly="True" Binding="{Binding Path=CategoryName}"/>
<sdk:DataGridTextColumn Header="Phone" IsReadOnly="True" Binding="{Binding Path=UnitId}" Visibility="Collapsed"/>
<!--<sdk:DataGridTextColumn Header="Notes" Binding="{Binding Comments}" IsReadOnly="False" />-->
<sdk:DataGridTemplateColumn x:Name="Notes1" Header="Notes">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock MaxWidth="200" TextAlignment="Left" TextWrapping= "Wrap" Text="{Binding Path=Comments, Mode=OneWay}" Width="200" />
<HyperlinkButton Name="btnEllipsis" Visibility="{Binding EllipsisVisibility,Source={StaticResource ViewModel}}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="15" Content="..." ToolTipService.ToolTip="Click For More" Command="{Binding NotesCommand,Source={StaticResource ViewModel}}" CommandParameter="{Binding}" >
</HyperlinkButton>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid>
and associated property in viewmodel as below
strong text
string _ellipsisVisibility;
public string EllipsisVisibility
{
get { return _ellipsisVisibility; }
set
{
if (_ellipsisVisibility != value)
{
_ellipsisVisibility = value;
RaisePropertyChanged("EllipsisVisibility");
}
}
}
and setting it to
EllipsisVisibility="Collapsed";
and
EllipsisVisibility="Visible";
There are two questions
1. It is not working to show or hide column
2. Need to show the hyperlink button in few rows and or hide in few rows based on some condition like Rowdatabound event in asp.net.
How to do can anyone please help???
(1) DataGrid column visibility is, unfortunately, not a dependency property. This means it can't engage in databinding. Some work-arounds to this involve subclassing the column type you're interested in.
(2) To show/hide a particular control, based on the data for that row, I would use a converter. Or create a new property on that entity type to directly bind to.
Well, I have couple of problem with DataGrid in WPF.
I have a Progress Bar in DataGrid which I want to show value from Database [Well complete DataGrid is bound to Datatable which I fetch from Database].
I have a Status as Enum in my code, and it's numeric value is store in database, though it is databind properly, I want to show Enum "string" rather than numeric value.
I didn't find if I rowDataBound which we use in ASP.NET, using that I can do this, but not sure how to do it now in WPF.
<DataGrid Height="125" Grid.Row="1" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="grdData" VerticalAlignment="Stretch" ItemsSource="{Binding}" IsReadOnly="True"
GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="#FFBEBEBE" VerticalGridLinesBrush="#FFBEBEBE" RowHeight="25" BorderThickness="0"
FontWeight="Bold" Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" AlternatingRowBackground="DarkGray"
Foreground="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" RowBackground="White" ColumnHeaderHeight="25" VerticalScrollBarVisibility="Visible"
BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}">
<DataGrid.Columns>
<DataGridTextColumn Header="Task Name" MinWidth="30" Width="100" Binding="{Binding Path=TaskName}" />
<DataGridTextColumn Header="Code" MinWidth="30" Width="50" Binding="{Binding Path=TaskCode}" />
<DataGridTextColumn Header="Status" Width="50" Binding="{Binding Path=Status}"/>
<DataGridTemplateColumn Header="Completed?" Width="*" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<ProgressBar x:Name="TaskProgress" HorizontalAlignment="Stretch" Value="{Binding Path=CompletePercentage}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
than in my C# code I put
grdData.DataContext = dt
Where dt is my Datatable filled using database simple select query.
Thanks.
I have datagrid that have rowdetails. My rows can be duplicated. I want to select all rows that are alike. how can I achieve that. I'm using mvvm pattern. I bind the selectedItem to the same property in my view model, but it does not work.
Here's the xaml code:
<DataGrid Margin="0,6,0,12"
BorderBrush="Silver"
SelectionMode="Single"
HeadersVisibility="Column"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserSortColumns="False"
CanUserReorderColumns="False"
VerticalGridLinesBrush="Silver"
HorizontalGridLinesBrush="Silver"
VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Auto"
RowDetailsVisibilityMode="Visible"
IsSynchronizedWithCurrentItem="{x:Null}"
ItemsSource="{Binding AccessoryWalls}"
Grid.Row="1"
Grid.Column="0"
Name="gAccessories">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID,StringFormat='W{0}'}" FontWeight="Bold" Foreground="Blue" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate >
<DataTemplate>
<Grid >
<DataGrid ItemsSource="{Binding Accessories}"
SelectedItem="{Binding Path=DataContext.SelectedAccessory, RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Mode=TwoWay}"
CanUserAddRows="False"
HeadersVisibility="Column"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Horizontal"
Width="60"
Binding="{Binding Horizontal, Converter={StaticResource DimensionConverter}}" />
<DataGridTextColumn Header="Vertical"
Width="60"
Binding="{Binding Vertical, Converter={StaticResource DimensionConverter}}" />
<DataGridTextColumn Header="Detail"
IsReadOnly="True"
Binding="{Binding LongName}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate />
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
As you see I bind it to "SelectedAccessory" on VM level and it works. It synchronizes with other controls in the screen, but it does not synchronizes with other rows in the datagrid.
I would like to select all "Overhead Door Opening12" rows when I clicked on one of them.
Thanks in advance.
I am working in a Silverlight 4 application. I have a datagrid that I would like to print. I am trying to use davidpoll printing functionality
Currently, I am printing just the datagrid header, but no items are being printed
<SLaB:CollectionPrinter x:Name="printer" ItemsSource="{Binding CurrentSearchView}" >
<SLaB:CollectionPrinter.BodyTemplate>
<DataTemplate>
<sdk:DataGrid x:Name="dgResults" VerticalAlignment="Stretch" SelectionMode="Single" HorizontalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding CurrentSearchView}" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Margin="10" FontSize="12" FontFamily="Segoe UI" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding PeopleId}" Header="People ID" Width="150"/>
<sdk:DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*" MinWidth="80"/>
<sdk:DataGridTemplateColumn Header="FQ" Width="80">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel x:Name="spFq" Background="{Binding FqBackground}" Margin="2">
<Image Source="{Binding FqImagePath}" Margin="4" Height="24" Width="24"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="LQ" Width="80" CellStyle="{StaticResource BrowseQualityCellStyle}">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel x:Name="spLq" Orientation="Horizontal" Background="{Binding LqBackground}" Margin="2" >
<myControls:PlayButtonControl x:Name="playControl" Margin="4" Height="24" Width="24" />
<Image Source="{Binding LqImagePath}" Margin="4" Height="16" Width="16"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Binding="{Binding Format}" Header="Format" Width="Auto"/>
<sdk:DataGridTextColumn Binding="{Binding Duration}" Header="Duration" Width="Auto"/>
<sdk:DataGridTextColumn Binding="{Binding Attachment}" Header="Attachment" Width="Auto"/>
<sdk:DataGridTextColumn Binding="{Binding Restore}" Header="Restore" Width="Auto"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</DataTemplate>
</SLaB:CollectionPrinter.BodyTemplate>
</SLaB:CollectionPrinter>
And here is the button command
<Button Command="{Binding PrintCommand, ElementName=printer}">Print</Button>
Note that both, the collectionprinter and datagrid are referencing the same property CurrentSearchView, which is a PagedCollectionView. I think that my problem lies there, maybe I am not able to used this kind of collection (i also try with an observable collection and found the same results), or my bindings are wrong (having both controls referencing the same property, doesn't seem right)
Any help will be greatly appreciated.
Thanks
I have found the problem. I needed to set the binding of the datagrid to "CurrentItems"
<sdk:DataGrid x:Name="dgResults" VerticalAlignment="Stretch" SelectionMode="Single" HorizontalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding CurrentItems}" IsReadOnly="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Margin="10" FontSize="12" FontFamily="Segoe UI" >
I have a short question:
I have a datagrid and binded it to something. Then I put a combobox in a column and binded it to another source by
ItemsSource="{Binding DataContext.Users, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
Now I want to use datagrid's source again, in the combobox again.That code doesn't work now and I know that there is a ID column in datagrid's source.
SelectedValue="{Binding ID}"
How can I fix it? Thanks in advance.
And here is my whole DataGrid and the problematic part is the combobox.
<DataGrid ItemsSource="{Binding SCs}" SelectedItem="{Binding SelectedSC}" Margin="0,10,0,0"
RowStyle="{StaticResource ResourceKey=DataGridRowStyle}" Style="{DynamicResource ResourceKey=UILDataGridStyle}" AutoGenerateColumns="False" EnableRowVirtualization="True"
RowDetailsVisibilityMode="VisibleWhenSelected" Width="Auto" IsReadOnly="True" Background="Transparent"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" GridLinesVisibility="Vertical" VerticalGridLinesBrush="{DynamicResource ResourceKey=UILBorderBrush}"
RowHeaderWidth="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" BorderBrush="{DynamicResource ResourceKey=UILBorderBrush}"
BorderThickness="1" SelectionUnit="FullRow" SelectionMode="Single" x:Name="SCDataGrid">
<DataGrid.Columns>
<DataGridTextColumn x:Name="securityConfigurationIDColumn" Binding="{Binding Path=SecurityConfigurationID}" Header="Security Configuration ID" Width="*" />
<DataGridTextColumn x:Name="securityConfigurationNameColumn" Binding="{Binding Path=SecurityConfigurationName}" Header="Security Configuration Name" Width="*" />
<DataGridTemplateColumn x:Name="proxyResponsibleUser_IDColumn" Header="Proxy Responsible User ID" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox IsEnabled="True" ItemsSource="{Binding DataContext.Users, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
SelectedValuePath="{Binding UserID}" DisplayMemberPath="{Binding FullName}" Width="117" Height="20"
SelectedValue="{Binding ProxyResponsibleUser_ID, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="responsibleUser_IDColumn" Binding="{Binding Path=User.FullName}" Header="Responsible User ID" Width="*" />
<DataGridTextColumn x:Name="securityConfigurationDescriptionColumn" Binding="{Binding Path=SecurityConfigurationDescription}" Header="Security Configuration Description" Width="*" />
<DataGridTextColumn x:Name="securityConfigurationURLColumn" Binding="{Binding Path=SecurityConfigurationURL}" Header="Security Configuration URL" Width="*" />
</DataGrid.Columns>
</DataGrid>
Use below cell template for combo box.
This code binds entire grid datasource to combo box template, which allow you use any property without doing the jugglery of finding ancestors..
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid DataContext="{Binding }">
<ComboBox IsEnabled="True"
ItemsSource="{Binding Users}"
SelectedValuePath="{Binding UserID}"
DisplayMemberPath="{Binding FullName}"
Width="117" Height="20"
SelectedValue="{Binding ProxyResponsibleUser_ID,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>