I'm learing WPF, it's fun but very confusing. I am currently trying to get values from DataTemplate. Simple Binding = {Binding Value} is not recognized. How can I get values of elements iside DataTemplate? Following code only displays Value:
<ScrollViewer>
<Grid x:Name="mainGrid" Margin="10,10,10,10">
<DataGrid x:Name="appSettingsData" AutoGenerateColumns="False" Grid.ColumnSpan="6" Grid.Row="7" CanUserAddRows="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Value" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="textBox">
<Setter Property="ContentTemplate">
<Setter.Value>
<!-- This is place from where I'm trying to take values -->
<DataTemplate>
<TextBox Text="{Binding Value}" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>
Why are you complicating things?
Your controls should be in the first DataTemplate itself:
<ScrollViewer>
<Grid x:Name="mainGrid" Margin="10,10,10,10">
<DataGrid x:Name="appSettingsData" AutoGenerateColumns="False" Grid.ColumnSpan="6" Grid.Row="7" CanUserAddRows="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Value" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="textBox">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>
Related
I need some help! I want to hide the DataGridTemplateColumn whenever the
DatagridTextColumn value is not null. I've done the triggering part but it only applies to the whole row, I just want to apply the hiding on a single cell. Thanks.
<DataGridTextColumn Header="Status" Binding="{Binding Status}"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="declined">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="accept" Height="20" FontSize="10" Tag="{Binding Id}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" CommandTarget="{Binding ElementName=isAccept}" Content="Accept" Click="AcceptLeave"/>
<Button Height="20" FontSize="10" Tag="{Binding Id}" x:Name="decline" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" CommandTarget="{Binding ElementName=isDecline}" Content="Decline" Click="DeclineLeave"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
i have a wpf c# app.
I am using the datagrid control.
for one of these cells I want to show a mutli-line tool-tip.
this is my code:
<DataGridTextColumn Header="{x:Static prop:Resources.Address}" Binding="{Binding Address}" >
<ToolTipService.ToolTip>
<StackPanel>
<TextBlock Text="Line#1" />
<TextBlock Text="Line#2" />
</StackPanel>
</ToolTipService.ToolTip>
</DataGridTextColumn>
But when I run this no tool-tip is displayed?
You could set the Tooltip property of the DataGridCell using a CellStyle:
<DataGridTextColumn Header="{x:Static prop:Resources.Address}" Binding="{Binding Address}" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<StackPanel>
<TextBlock Text="Line#1" />
<TextBlock Text="Line#2" />
</StackPanel>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
I'm working in wpf and I am using below data-grid:
<DataGrid Name="dgUseCaseList" AutoGenerateColumns="False" Grid.Row="0" CanUserAddRows="False" VirtualizingStackPanel.IsVirtualizing="False" Height="620" VerticalAlignment="Top" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTemplateColumn Width="30">
<DataGridTemplateColumn.Header>
<CheckBox Name="chkSelectAllUseCases" Click="chkSelectAllUseCases_Click" IsHitTestVisible="False"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkSelectUseCase" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" Click="chkSelectUseCase_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="100" Header="UseCaseId" Binding="{Binding Path=UseCaseId}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding UseCaseDescription}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
But still I am able to select multiple check-boxes. How to disable this. (I want to select just one checkbox. On selection of next checkbox,the previous one should get unchecked)
Try RadioButton instead of CheckBox.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="group1" Name="chkSelectUseCase" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" Click="chkSelectUseCase_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
You can customize RadioButton's Template if you want.
<Style TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<CheckBox
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=IsChecked, Mode=TwoWay}"
IsHitTestVisible="False" Content="{TemplateBinding Content}" />
<CheckBox
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=IsChecked, Mode=TwoWay}"
Content="{TemplateBinding Content}" Opacity="0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I am working on a system that manages documents and I currently implement a text search. The result shall be displayed in a list view similar to:
Each hit shall be displayed in a combined row with some information where the tit was found and a short embedded preview. The columns shall be resizable and the text shall be aligned with the width of the columns.
I have seen some examples how this can be done in Windows Forms but not using WPF. Can anybody point me to an example how to create a list view with sch type of rows.
Greetings
Clemens
I figured out that I can do it with datagrid that I style for my needs.
<Window x:Class="DataGridExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridExample"
mc:Ignorable="d"
Title="Hit List" Height="350" Width="350">
<Window.Resources>
<!-- Hover state brush -->
<SolidColorBrush x:Key="HoverBackgroundBrushKey" Color="#E5F3FB" />
<SolidColorBrush x:Key="HoverBorderBrushKey" Color="#70C0E7" />
<!-- Select (activate) the state of the brush -->
<SolidColorBrush x:Key="SelectedActiveBackgroundBrushKey" Color="#CBE8F6" />
<SolidColorBrush x:Key="SelectedActiveBorderBrushKey" Color="#26A0DA" />
<!-- Select (hover) state brush -->
<SolidColorBrush x:Key="SelectedHoverBackgroundBrushKey" Color="#D1E8FF" />
<SolidColorBrush x:Key="SelectedHoverBorderBrushKey" Color="#66A7E8" />
<!-- Select the (failed) status brush -->
<SolidColorBrush x:Key="SelectedInactiveBackgroundBrushKey" Color="#F7F7F7" />
<SolidColorBrush x:Key="SelectedInactiveBorderBrushKey" Color="#DEDEDE" />
</Window.Resources>
<Grid>
<DataGrid x:Name="HitList" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False"
GridLinesVisibility="None"
RowDetailsVisibilityMode="Visible"
RowHeaderWidth="0"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ItemsSource="{Binding Path=Hits}">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="BackgroundBorder" Background="Transparent">
<ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource HoverBackgroundBrushKey}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource SelectedActiveBackgroundBrushKey}" />
<Setter Property="BorderBrush" Value="{StaticResource SelectedActiveBackgroundBrushKey}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource SelectedInactiveBackgroundBrushKey}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Document" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DocumentName, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Field" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Field, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Location" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Location, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Length, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="2,2,8,2"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsPresenter}}, Path=ActualWidth}"
HorizontalAlignment="Left" >
<TextBlock Margin="24,0,0,8" TextWrapping="Wrap" HorizontalAlignment="Left">
<TextBlock.Inlines>
<Run Text="{Binding Path=MatchEnvironment.Head}" FontStyle="Italic" Foreground="DarkGray"/>
<Run Text="{Binding Path=MatchEnvironment.Hit}" Background="Yellow" Foreground="Blue" FontWeight="Bold" FontStyle="Italic"/>
<Run Text="{Binding Path=MatchEnvironment.Tail}" FontStyle="Italic" Foreground="DarkGray"/>
</TextBlock.Inlines>
</TextBlock>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
I have a problem with setting datagrid event.
I want to register the datagrid inside the ListView with the following event :
CellEditEnding="dataGridInstallationProperties_CellEditEnding"
However, since i set the DataContext of this datagrid on the fly, the application throws NulleferenceException
Is there a way to get this collection of datagrids inside the list view and set this event programatically in C# for each one of them?
Below is my xaml :
<ListView x:Name="listViewInstallProperties"
Height="Auto"
Width="Auto"
ItemsSource="{Binding InstallPackage.Features.FeaturesList}"
BorderBrush="{x:Null}" Foreground="{x:Null}"
Background="{x:Null}">
<ListView.Resources>
<DataTemplate x:Key="ExpanderInstallPropsItemTemplate">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Expander x:Name="expanderInstallationProperties" Header="{Binding DisplayName}"
Visibility="{Binding Path=IsSelected, Converter={StaticResource boolToVis}, Mode=TwoWay}"
Grid.Column="0" HorizontalAlignment="Left"
Margin="0,0,0,-0.001"
d:LayoutOverrides="Height, GridBox" Width="600">
<DataGrid x:Name="dataGridInstallationProperties"
CellEditEnding="dataGridInstallationProperties_CellEditEnding"
CanUserAddRows="False" AutoGenerateColumns="False"
ItemsSource="{Binding Path=Properties}"
Margin="0" RenderTransformOrigin="-1.6,-1.231" Background="White"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" VerticalContentAlignment="Top"
d:LayoutOverrides="GridBox" BorderBrush="{x:Null}" RowBackground="{x:Null}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Description}"
Header="Property" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Value, UpdateSourceTrigger=PropertyChanged}"
Header="Value" />
<DataGridTemplateColumn Header="Restore"
CellTemplate="{StaticResource RestoreDefaultPropertyButtonTemplate}" />
</DataGrid.Columns>
</DataGrid>
</Expander>
</ScrollViewer>
</DataTemplate>
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="{x:Null}" />
<Setter Property="BorderBrush"
Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Extend The Feature To Get Its Related Properties" CellTemplate="{StaticResource ExpanderInstallPropsItemTemplate}" />
</GridView>
</ListView.View>
Thanks !