My task is to set the Expander's TextBlock width based on a column of the datagrid which is contained in. The XAML is this:
<DataGrid x:Name="GridData" Grid.Column="4" Height="287" ColumnWidth="Auto" CanUserResizeColumns="False">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Background="Red" HorizontalAlignment="Left" HorizontalContentAlignment="Left">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Width="{Binding ElementName=GridData.colTimeAlarm, Path=ActualWidth}" Background="Azure" Text="{Binding StringFormat=hh\\:mm\\:ss, Converter={StaticResource SumTimes}, ConverterParameter=TimeAlarm}" />
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Width="Auto" MinWidth="80" Binding="{Binding Data, UpdateSourceTrigger=PropertyChanged}" Header="#_151_Data" Foreground="Black" IsReadOnly="True" x:Name="colTimeAlarm" />
</DataGrid.Columns>
In the TextBlock I binded the width property as ActualWidth of the colum specified but it doesn't work. What's wrong?
Related
I currently have a datagrid in WPF with only two columns, and its item source is set within the .cs codes when attempting to bind it to its specific item source, but initially it is not referenced to any itemsource. The problem is, if there is no item source yet, the columns are squeezed up even though i have specified the width of the columns. (in attached picture)
The columns are looking fine until i added the <datagrid.groupstyle> portion, may i ask if i am missing something or am i doing something wrong.
Below is the codes inside my .xaml for this datagrid.
<DataGrid Visibility="Visible"
x:Name="IFCInfoDataGrid" Width="auto" Height="auto" HeadersVisibility="Column"
AutoGenerateColumns="False" Margin="12" IsReadOnly="True" Grid.ColumnSpan="2"
SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="False" >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<DockPanel Background="LightBlue">
<TextBlock Text="{Binding Path=Name}" Foreground="Blue" Margin="30,0,0,0" Width="100" />
</DockPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}" >
<Setter Property="Margin" Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander
BorderBrush="#FF002255"
IsExpanded="True"
Background="Tan"
BorderThickness="0,0,0,1">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontWeight="Bold" Text="{Binding Path=Name}" Width="200" />
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding IFCParameter}" Header="IFC Parameter" Width="0.5*"
ElementStyle="{StaticResource DataGridRowStyle}" IsReadOnly="True" CanUserSort="False"/>
<DataGridTextColumn Binding="{Binding ParameterValue}" Header="Value" Width="0.5*"
ElementStyle="{StaticResource DataGridRowStyle}" IsReadOnly="True" CanUserSort="False"/>
</DataGrid.Columns>
</DataGrid>
Set the <GroupStyle.Panel> element to an ItemsPanelTemplate with a DataGridRowsPresenter:
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
<GroupStyle.HeaderTemplate>
...
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
...
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
I'm facing two problems with my projet:
SpackPanel not expanding the space available inside DataGridTemplateColumn
TextBlock not wrapping
I have a DataGrid with three different columns, and I want the one in the middle to use all available space left. Also in that column I have a StackPanel because I need to have two elements inside:
Border with icon inside that just appears depending on Use_Half_Portion value
TextBlock showing text
Giving background colors to the elements I have noticed that the StackPanel doesn't fill all of the available space left insite the column, and the wrap in the TextBlock is not being respected. Im supposing that the StackPanel is not assuming the MaxWidth of the DataGridTemplateColumn and it's using unlimited space, and because of that the TextBlock assumes that the limit is not being reached to make the text wrap.
Here's my code:
<DataGrid x:Name="ConsumptionList" ItemsSource="{Binding ConsumptionList}"
AutoGenerateColumns="False" IsReadOnly="True"
CanUserReorderColumns="False" CanUserResizeColumns="False"
BorderBrush="{x:Null}" VerticalContentAlignment="Center" CanUserSortColumns="False" HeadersVisibility="Column" Margin="0,0,0,-4" Background="{x:Null}">
<DataGrid.Resources>
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource foScrollBar.Small}"/>
</DataGrid.Resources>
<DataGrid.ColumnHeaderStyle>
<StaticResource ResourceKey="clientScreenList.Header"/>
</DataGrid.ColumnHeaderStyle>
<DataGrid.RowStyle>
<StaticResource ResourceKey="clientScreenList.Lines"/>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<StaticResource ResourceKey="clientScreenList.Cells"/>
</DataGrid.CellStyle>
<DataGrid.Style>
<StaticResource ResourceKey="foBillingList"/>
</DataGrid.Style>
<DataGrid.Columns>
<!--quantity-->
<DataGridTextColumn x:Name="ConsQttColumn" MinWidth="20" Width="Auto" MaxWidth="80" FontFamily="Segoe UI Semibold"
Binding="{Binding Quantity, Converter={StaticResource DecimalToQuantityConverter}}" Header="{x:Static localization:LanguageRes.consList_quantity}" />
<!--description-->
<DataGridTemplateColumn x:Name="ConsDescriptionColumn" Header="Descrição" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Background="#FFDC1A1A">
<Border CornerRadius="30" Grid.Column="0" Width="30" Height="30" Background="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,3,0">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Use_Half_Portion}" Value="1">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<TextBlock Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Center" Style="{StaticResource icon}" TextAlignment="Center" FontSize="22" Text="{x:Static TechUI:AppConstants+ICONS.HalfDose}" Padding="0"
Margin="0" Foreground="Black" Background="{x:Null}" />
</Border>
<TextBlock Text="{Binding Product_Description}" HorizontalAlignment="Left" TextAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" FontFamily="Segoe UI Semibold" Background="#FFB4AD1C" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- total price-->
<DataGridTemplateColumn x:Name="ConsTotalColumn" Header="Preço Total" Width="Auto" MaxWidth="135">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" MaxWidth="135" VerticalAlignment="Center" >
<TextBlock MaxWidth="135" HorizontalAlignment="Stretch" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" FontFamily="Segoe UI Semibold" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="{Binding Total_W_Vat, Converter={StaticResource DecimalToPriceConverter}}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Is_Composite}" Value="1">
<Setter Property="Text" Value=" " />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
As you can see in the image, the red element is the StackPanel and the green element is the TextBlock. In the first line you can see that the text is being cut and not being wrapped, the full text is "Almondegas Frango"
I have found a solution.
Just changed the StackPanel for a DockPanel and it worked the exact same way I wanted!
Just checked this out and it's very similar, but for vertical scrolling not horizontal.
I have a datagrid with grouped data, looks like this:
I just want to keep the group header information visible as I scroll horizontally across through the columns. But the header scrolls off the window:
Here's the xaml (originally from here), only for the datagrid group style... if you need more of it lemme know!
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=ReferralDate}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupItem}},
Converter={StaticResource ResourceKey=groupTitleVisitsConverter}}"
Foreground="White" FontWeight="Bold"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
I am working with the Datagrid grouping example here at MSDN. The code in the example uses an Expander to display child rows of a group. I don't want to use an Expander in my code. I want to always display every row. How can I display the child rows in a grouped datagrid without using an Expander control?
Instead of using Expander you can use Border.
<DataGrid.GroupStyle>
<!-- Style for groups at top level. -->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Border BorderThickness="1" BorderBrush="Black" CornerRadius="5,5,5,5" Margin="0,0,0,5">
<StackPanel>
<StackPanel Height="30" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100" VerticalAlignment="Center"/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" VerticalAlignment="Center" />
</StackPanel>
<ItemsPresenter />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
<!-- Style for groups under the top level. -->
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<DockPanel Background="LightBlue">
<TextBlock Text="{Binding Path=Name, Converter={StaticResource completeConverter}}" Foreground="Blue" Margin="30,0,0,0" Width="100"/>
<TextBlock Text="{Binding Path=ItemCount}" Foreground="Blue"/>
</DockPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
Here's the example from MSDN.
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" BorderBrush="#FFA4B97F"
BorderThickness="0,0,0,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"
Margin="5,0,0,0" Width="100"/>
<TextBlock FontWeight="Bold"
Text="{Binding Path=ItemCount}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
How can I make it occupy the whole width of the listbox? (HorizontalScrollBar is disabled)?
What I exactly need is DockPanel to be streched.
You have to include the reference of PresentationFramework.Aero in your project.
After this in your ListBox you have to insert this attribute:
<ListBox
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
HorizontalContentAlignment="Stretch"
>
</ListBox>