I am creating this application, I want that Groupbox fit the available space and with margin of 10 in bottom, This is my code :
<StackPanel Orientation="Vertical" Height="auto">
<GroupBox x:Name="group" Margin="5,10,0.2,0" Height="300" >
<DataGrid x:Name="Data" Margin="348,81,348.4,44.8" HorizontalAlignment="Center" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="200"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="200"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="250"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="250"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</StackPanel>
I have set the height of Groupbox, I am using the app in laptop but when I[! try to use it in desktop it doesn't fit
Replace the stack panel with a grid, and then change the GroupBox height to auto.
<Grid>
<GroupBox x:Name="group" Margin="5,10,0.2,0" Height="auto">
<DataGrid x:Name="Data" Margin="348,81,348.4,44.8" HorizontalAlignment="Center" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="200"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="200"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="250"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="250"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</Grid>
Lose the StackPanel and replace it with a grid, and set the Vertical alignment of the GroupBox to bottom:
<Grid>
<GroupBox x:Name="group" Margin="0,0,0,10" Height="300" VerticalAlignment="Bottom">
<DataGrid x:Name="Data" Margin="348,81,348.4,44.8" HorizontalAlignment="Center" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="200"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="200"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="250"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="250"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="data" Width="50"/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</Grid>
Remove Margin="348,81,348.4,44.8" HorizontalAlignment="Center" from <DataGrid x:Name="Data" Margin="348,81,348.4,44.8" HorizontalAlignment="Center" > .
When you specifically assign margin and horizontal alignment, its constrained. So, remove those two values.
set the vertical alignment to bottom. VerticalAlignment = "Bottom"
Related
I want to align my columns in the datagrid. How should I do this? I also used HorizontalContentAlignment = "Center" but it did not work. Thank you for your help
<DataGrid Margin="0,69,0,0" x:Name="dgv_Useradmin" FlowDirection="RightToLeft"
CanUserDeleteRows="False" CanUserResizeRows="False" CanUserReorderColumns="False" ColumnWidth="*" Background="Azure"
FontFamily="B Roya" FontWeight="Bold" FontStretch="UltraCondensed" BorderBrush="#29bb89" OpacityMask="#e84545" HorizontalAlignment="Center"
CanUserAddRows="False" AlternatingRowBackground="Bisque" IsReadOnly="True" AutoGenerateColumns="False" HorizontalContentAlignment="Center"
RowDetailsVisibilityMode="Visible" SelectedIndex="0" LoadingRow="Dgv_Useradmin_OnLoadingRow">
<!--DataColumn-->
<DataGrid.Columns>
<DataGridTextColumn Header="RN" Width="65" Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={convertor:RowToIndexConverter}}"/>
<DataGridTextColumn Header="LogID" Binding="{Binding LogID}" Visibility="Hidden"/>
<DataGridTextColumn Header="Username" Binding="{Binding Username }"/>
<DataGridTextColumn Header="Pass" Binding="{Binding PassWord}" Visibility="Hidden"/>
<DataGridTextColumn Header="Number" Binding="{Binding NumberPhone}"/>
</DataGrid.Columns>
Shown for one column.
For the rest of the columns, the same is true.
<DataGrid.Columns>
<DataGridTemplateColumn Header="RN" Width="65">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={convertor:RowToIndexConverter}}"
TextAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<!--If editing is envisaged-->
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={convertor:RowToIndexConverter}}"
HorizontalContentAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="LogID" Binding="{Binding LogID}" Visibility="Hidden"/>
<DataGridTextColumn Header="Username" Binding="{Binding Username }"/>
<DataGridTextColumn Header="Pass" Binding="{Binding PassWord}" Visibility="Hidden"/>
<DataGridTextColumn Header="Number" Binding="{Binding NumberPhone}"/>
</DataGrid.Columns>
To center the heading, you need to give it a style:
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridTemplateColumn.HeaderStyle>
I have a WPF application that contains a datagrid and nested datagrid (RowDetailsTemplate).
For each row have a datagrid that have 1000 rows inside.
I see that nested datagrid have a low performance while i open that.
I also try to set grid height and VirtualizingStackPanel.IsVirtualizing="True", VirtualizingStackPanel.VirtualizationMode="Recycling".
but without successes..
The xaml:
<Window x:Class="TestMasterDetails.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
ItemsSource="{Binding Persons}"
AutoGenerateColumns="False"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="+" Click="HandleExpandCollapseForRow"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" Width="100"/>
<DataGridTextColumn Header="LastName" Binding="{Binding LastName}" Width="100"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid Height="400">
<DataGrid
ItemsSource="{Binding Addresses}"
AutoGenerateColumns="False"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling" >
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
The MainWindow code behind:
public partial class MainWindow : Window
{
VM.VM _vm = new VM.VM();
public MainWindow()
{
InitializeComponent();
DataContext = _vm;
}
private void HandleExpandCollapseForRow(object sender, RoutedEventArgs e)
{
Button expandCollapseButton = (Button)sender;
DataGridRow selectedRow = DataGridRow.GetRowContainingElement(expandCollapseButton);
if (null != expandCollapseButton && "+" == expandCollapseButton.Content.ToString())
{
selectedRow.DetailsVisibility = Visibility.Visible;
expandCollapseButton.Content = "-";
}
else
{
selectedRow.DetailsVisibility = Visibility.Collapsed;
expandCollapseButton.Content = "+";
}
}
}
The ViewModel:
public class VM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
List<PersonModel> _persons = new List<PersonModel>();
public List<PersonModel> Persons
{
get { return _persons; }
set
{
if (_persons != value)
{
_persons = value;
OnPropertyChanged("Persons");
}
}
}
const int ZERO = 0;
const int LENGTH = 1000;
public VM()
{
for (int i = ZERO; i < LENGTH; i++)
{
var person = new PersonModel() { FirstName = "FirstName" + i.ToString(), Id = i, LastName = "LastName" + i.ToString() };
for (int j = ZERO; j < LENGTH; j++)
{
person.Addresses.Add(new AddressModel() { Id = j, Address = "Address" + j.ToString() });
}
_persons.Add(person);
}
}
}
I have WPF form with a DataGrid in it. When the form loads or when it doesn't have focus, there is a white colored highlight on the first cell. If I click on the gridview or when the form gets focus, the current cell becomes blue (the selection color that I set). See pictures:
'Lost Focus
Got Focus
I want to remove the white highlighting when there is no focus on the datagrid, i.e. it should look like the other cells in that row. What is the property or event that I must handle?
<DockPanel Name="DocHolder" >
<DataGrid x:Name="dataGrid1" MaxHeight="100" AutoGenerateColumns="False" HeadersVisibility="Column" SelectionUnit="Cell" FontSize="11"
VerticalContentAlignment="Center"
HorizontalGridLinesBrush="#FFC7BDBD" AlternatingRowBackground="#690BB9F8"
EnableColumnVirtualization="False" EnableRowVirtualization="False" GridLinesVisibility="None"
CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
ClipboardCopyMode="ExcludeHeader" HorizontalScrollBarVisibility="Disabled" MinRowHeight="20" OverridesDefaultStyle="False"
Style="{DynamicResource scGrid}" ColumnHeaderStyle="{DynamicResource scGridHeader}">
<DataGrid.BorderBrush> <SolidColorBrush /> </DataGrid.BorderBrush>
<DataGrid.RowBackground> <SolidColorBrush /></DataGrid.RowBackground>
<DataGrid.Background> <SolidColorBrush /> </DataGrid.Background>
<DataGrid.Columns>
<DataGridTextColumn Header="GID" DisplayIndex="0" Visibility="Hidden" Binding="{Binding GID}" />
<DataGridTextColumn Header="Region" DisplayIndex="1" IsReadOnly="True" Binding="{Binding AreaName}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridTextColumn Header="Grps" DisplayIndex="2" IsReadOnly="True" Binding="{Binding GName}" CellStyle="{StaticResource FocusAndShowElementData}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridTextColumn Header="Thk" DisplayIndex="3" Binding="{Binding Thick}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="TX" DisplayIndex="4" Binding="{Binding TopX}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="TY" DisplayIndex="5" Binding="{Binding TopY}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="BX" DisplayIndex="6" Binding="{Binding BottomX}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridTextColumn Header="BY" DisplayIndex="7" Binding="{Binding BottomY}" IsReadOnly="True" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridComboBoxColumn Header="DTX" DisplayIndex="8" TextBinding ="{Binding BTopX, Mode=TwoWay}" x:Name="cmbBTopX" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="STX" DisplayIndex="9" Binding="{Binding BTopX, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridComboBoxColumn Header="DTY" DisplayIndex="10" TextBinding ="{Binding BTopY, Mode=TwoWay}" x:Name="cmbBTopY" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="STY" DisplayIndex="11" Binding="{Binding BTopY, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
<DataGridComboBoxColumn Header="DBX" DisplayIndex="12" TextBinding ="{Binding BBottomX, Mode=TwoWay}" x:Name="cmbBBottomX" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="SBX" DisplayIndex="13" Binding="{Binding SBottomX, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}"/>
<DataGridComboBoxColumn Header="DBY" DisplayIndex="14" TextBinding ="{Binding BBottomY, Mode=TwoWay}" x:Name="cmbBBottomY" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" ElementStyle="{StaticResource scDGCombo}"/>
<DataGridTextColumn Header="SBY" DisplayIndex="15" Binding="{Binding BBottomY, Mode=TwoWay}" CellStyle="{StaticResource CellGotFocus}" HeaderStyle="{StaticResource HeaderStyle}" />
</DataGrid.Columns>
</DataGrid>
</DockPanel>
You have to override the cell template of datagrid cell
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This is just an example that you can refer, Like this you can do whatever template you want
Hope this helps.
For WPF datagrid I am using following lines of code in xaml file. I have set the resize property of columns in datagrid to true. Still it doesn't show splitter icon on columns of datagrid.
<WPFToolkit:DataGrid CanUserAddRows="False" Background="GhostWhite"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
MouseDoubleClick="gridSearchClinet_MouseDoubleClick"
CanUserResizeColumns="True" CanUserReorderColumns="False"
Sorting="gridSearchClinet_Sorting" CanUserResizeRows="False"
ItemsSource="{Binding}" IsReadOnly="True"
CanUserSortColumns="True" CanUserDeleteRows="False"
x:Name="gridSearchClinet" AutoGenerateColumns="False"
Margin="10,65,11,50">
<WPFToolkit:DataGrid.RowStyle>
<Style TargetType="{x:Type WPFToolkit:DataGridRow}">
<Style.Triggers>
<Trigger Property="WPFToolkit:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</WPFToolkit:DataGrid.RowStyle>
<WPFToolkit:DataGrid.Columns>
<WPFToolkit:DataGridTextColumn Header="Division" Width="70"
CanUserResize="True" CanUserReorder="False"
CanUserSort="True" Binding="{Binding Spart}" />
<WPFToolkit:DataGridTemplateColumn Header="Description" CanUserResize="True"
Width="325" CanUserSort="True"
CanUserReorder="False"
SortMemberPath="Description">
<WPFToolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label HorizontalAlignment="Left" Width="1000"
VerticalAlignment="Center" BorderThickness="0"
Content="{Binding Path=Description}" FontSize="9"
ToolTip="{Binding Path=Description}" />
</DataTemplate>
</WPFToolkit:DataGridTemplateColumn.CellTemplate>
</WPFToolkit:DataGridTemplateColumn>
<WPFToolkit:DataGridTextColumn Header="PG" Width="50" CanUserResize="True"
CanUserReorder="False" CanUserSort="True"
Binding="{Binding Konda}" />
<WPFToolkit:DataGridTextColumn Header="BCC" Width="50" CanUserResize="True"
CanUserReorder="False" CanUserSort="True"
Binding="{Binding IhKomp}" />
<WPFToolkit:DataGridTemplateColumn Header="Booking Unit" CanUserResize="True"
Width="735" CanUserSort="True"
SortMemberPath="Beinh">
<WPFToolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label HorizontalAlignment="Left" Width="1000"
VerticalAlignment="Center" BorderThickness="0"
Content="{Binding Path=Beinh}" FontSize="9"
ToolTip="{Binding Path=Beinh}" />
</DataTemplate>
</WPFToolkit:DataGridTemplateColumn.CellTemplate>
</WPFToolkit:DataGridTemplateColumn>
</WPFToolkit:DataGrid.Columns>
</WPFToolkit:DataGrid>
Any help is highly appreciated.
I've got a DataGrid with a DataTrigger setup, so when it's value is "UP" I'd like the trigger to only change the background in the 'directionColumn' datagridcolumn only.
What I've got so far updates the entire row. I also tried placing 'targetname' in the setter element but that just gives an error. Please help.
XAML:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Name="summaryReportDataGridDATE" RowDetailsVisibilityMode="VisibleWhenSelected" Width="330">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Direction}" Value="UP">
<Setter>
<Setter.Property>Background</Setter.Property>
<Setter.Value>Red</Setter.Value>
<!--<Setter Property="Background" Value="Red" TargetName="directionColumn"/>-->
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<!--<DataGridTemplateColumn x:Name="dateColumn" Header="Date" Width="100" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=Date, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>-->
<DataGridTextColumn x:Name="dateColumn" Binding="{Binding Path=Date, StringFormat={}{0:dd/MM/yyyy}}" Header="Date" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"/>
<DataGridTextColumn x:Name="closeColumn" Binding="{Binding Path=ClosingPrice}" Header="ClosingPrice" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
<DataGridTextColumn x:Name="directionColumn" Binding="{Binding Path=Direction}" Header="Direction" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
<DataGridTextColumn x:Name="resultColumn" Binding="{Binding Path=Result}" Header="Result" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
</DataGrid.Columns>
</DataGrid>
How about a TemplateColumn instead of custom cell style:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Name="summaryReportDataGridDATE" RowDetailsVisibilityMode="VisibleWhenSelected" Width="330">
<DataGrid.Columns>
<DataGridTextColumn x:Name="dateColumn" Binding="{Binding Path=Date, StringFormat={}{0:dd/MM/yyyy}}" Header="Date" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"/>
<DataGridTextColumn x:Name="closeColumn" Binding="{Binding Path=ClosingPrice}" Header="ClosingPrice" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
<DataGridTemplateColumn x:Name="directionColumn" Header="Direction" Width="80" CanUserReorder="False" CanUserSort="False" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="directionColumnTextBlock" Text="{Binding Path=Direction}" Width="80" FontFamily="Arial Rounded MT"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Direction}" Value="UP">
<Setter TargetName="directionColumnTextBlock" Property="Background" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="resultColumn" Binding="{Binding Path=Result}" Header="Result" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
</DataGrid.Columns>
</DataGrid>