WPF GridView Column width on different PC resolution - wpf

How to set width for GridViewColumn that it will work all time on different resolution? For example I have this listView:
And then I open it in another PC (with different resolution) I get this view :
It is missing few column.
I copy one of my column xaml code :
<ListView x:Name="sample" Margin="30,98.4,362,150" ScrollViewer.VerticalScrollBarVisibility="Visible"
AlternationCount="2" ScrollViewer.CanContentScroll="False">
<ListView.View>
<GridView>
<GridViewColumn>
<!-- 1st Column-->
<GridViewColumnHeader Content="Product" Margin="10,0,0,0"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Grid Height="42px">
<TextBlock Text="{Binding odd}" VerticalAlignment="Center"
Width="600" TextAlignment="Left"/>
</Grid>
<TextBlock Foreground="#B30C0C" Height="42px" Padding="10,5,0,0" Text="{Binding discounText}"
Visibility="{Binding IsDiscount, Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn >
How you see I write width parameter in TextBlock. How I have to rewrite it or maybe make some changes in code?

Use relative sizing (with *) instead of fixed values. For that, you'll have to move your Width setting from the TextBlock to the Column itself:
<GridViewColumn Width="600*">
<!-- 1st Column-->
<GridViewColumnHeader Content="Product" Margin="10,0,0,0"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Grid Height="42px">
<TextBlock Text="{Binding odd}" VerticalAlignment="Center"
TextAlignment="Left"/>
</Grid>
<TextBlock Foreground="#B30C0C" Height="42px" Padding="10,5,0,0" Text="{Binding discounText}"
Visibility="{Binding IsDiscount, Converter={StaticResource VisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I've set 600* because the fastest way to keep the current proportions is keeping the same values you have right now, but adding the *. But that number can be a lot lower, since it just defines the proportion between columns... Feel free to toy with the values to get the effect you want.

Related

How to remove blank space between column of listview wpf?

I want to remove blank between Symbol column and Price column
HorizontalAlignment doesn't work
My code:
<GridViewColumn Width="80" Header="Symbol">
<GridViewColumn.CellTemplate >
<DataTemplate>
<Grid Width="200" Background="Blue" Margin="0,0,0,0" Height="25" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Symbol}" Width="auto" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Price" Width="90">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastClose, StringFormat={}{0:N2}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Its not ideal, but have you tried setting a negative right margin E.g "0,0,-10,0" or tried adjusting the Padding property on the Grid.
As a side note, i'm not sure you neew Width="auto" in the TextBlock.

DataContext of parent

I am a newbie WPF. I need to acheive the following: I have a ModelView which contains Observable collection of class "Edata". Edata also contains another ObservableColelction of Class"eParams" which contains 4 properties.
now I have listbox which contains the list of Edata, and another listview which contains the params .
Every thing works fine. the challenge is the tool tip. I have in the Edata Class Property called AsStringToolTip. I use this property to give some hint to user and briefed info about the row where the mouse is over.
<ListBox x:Name="lbx1" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding EData}" VerticalAlignment="Center" HorizontalAlignment="Center">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource Description}" TextWrapping="Wrap">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} , {1}">
<Binding Path="Edata.category" />
<Binding Path="Edata.EId" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- No Compar ListView -->
<ListView Grid.Column="1" Grid.Row="1" ItemsSource="{Binding SelectedItem.Edata.eparams ,ElementName=lbx1}" Grid.IsSharedSizeScope="True" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingPanel.VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
<GridView >
<GridViewColumn Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Name}">
<TextBlock.ToolTip>
**<TextBlock DataContext="{Binding SelectedValue,ElementName=lbx1}" Text="{Binding Path=AsStringToolTip}">**
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ValueString" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding ValueString}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="value" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding value}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="paramtype">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding paramtype}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I hope I am clear enough. any Advise. Currently the tool tip shows nothing!!!
Since the Tooltip resides in its own visual tree it cannot find the ListBox when the binding is evaluated.
But you could bind the Tag property of the TextBlock to the ListBox and then bind the element in the Tooltip to the PlacementTarget of the ToolTip itself. It's probably better explained with some sample markup:
<GridViewColumn Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="Name" Tag="{Binding ElementName=lbx1}">
<TextBlock.ToolTip>
<ToolTip>
<TextBlock Text="{Binding PlacementTarget.Tag.SelectedItem.AsStringToolTip,
RelativeSource={RelativeSource AncestorType=ToolTip}}" />
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The PlacementTarget in the binding path refers to the "Name" TextBlock.
The Tag property of this TextBlock returns a reference to the "lbx1" ListBox.
You can then get the AsStringToolTip property of the currently selected Edata object.

Adding additional padding with Width="Auto"

I have a Listview with multiple GridViewColumns, I am using Width="Auto" for each of the columns, this ensures that the columns are sized according to their largest element.
I want to add additional breathing space for each column header, something along the lines of:
Width = "Auto" + 30
I have tried setting Margins and padding however this results in the Column text heading being cut off prematurely when resized.
Any ideas on how I can achieve this ?
My GridViewColumn currently ?
<GridViewColumn Width="Auto">
<GridViewColumn.Header>
<GridViewColumnHeader HorizontalContentAlignment="Left"
Content="{x:Static resources:Resources.TableHeadingDescription}"
SizeChanged="Description_Column_SizeChanged">
</GridViewColumnHeader>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="DescriptionTextBlock"
Text="{Binding Description}"
Style="{StaticResource TextBlock_GridEntries_Style}"
Margin="{Binding HierarchyLevel, Mode=OneWay, Converter={StaticResource HierarchyToMarginConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
add ContentTemplate to GridViewColumnHeader, and in template set some Margin on element
<GridViewColumn Width="Auto">
<GridViewColumn.Header>
<GridViewColumnHeader HorizontalContentAlignment="Left"
Content="{x:Static resources:Resources.TableHeadingDescription}"
SizeChanged="Description_Column_SizeChanged">
<GridViewColumnHeader.ContentTemplate>
<DataTemplate>
<Label Margin="30,0" Content="{Binding}" />
</DataTemplate>
</GridViewColumnHeader.ContentTemplate>
</GridViewColumnHeader>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="DescriptionTextBlock"
Text="{Binding Description}"
Style="{StaticResource TextBlock_GridEntries_Style}"
Margin="{Binding HierarchyLevel, Mode=OneWay, Converter={StaticResource HierarchyToMarginConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

How to set tabindex in dynamic generated textbox inside listView in wpf

We built our user interface from XML definitions (not XAML) but underneath we use a WPF to present the UI. That is at runtime, we create the WPF UI based on our XML definition.
<ListView ItemsSource="{Binding}" Width="400px" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Name="ListView"
ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.ColumnSpan="3"
SelectionChanged="ListView_SelectionChanged" BorderThickness="0" IsTabStop="False">
<ListView.View>
<GridView x:Name="grid">
<GridViewColumn Width="100px">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ModuleName}" Width="100px" Foreground="Black" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="200px">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ModuleUserCount, TargetNullValue=''}" MaxLength="6" Name="txtModuleUserCount" KeyDown="txtModuleUserCount_KeyDown" MinWidth="180" MaxWidth="200" BorderBrush="Gray"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView.View>
</ListView>
I generate 4 module through XML SO UI have four TextBox in these dynamic TextBox tabIndex is not working. I tried KeyboardNavigation.TabNavigation="Cycle" but it's not working. How to make tab navigation work for this layout?
I know its too too late, but I would like to answer. I had the same problem. I tried following way and it resolved my problem: Set:
KeyboardNavigation.TabIndex="10"
or whatever value you require in ListView itself.

TreeView two way binding failing at lower levels

Ok I have a custom TreeListView that inherits from TreeView and has some columns.
I have custom objects in my application with two text properties; KeyValue and Description. I'm trying to bind textboxes in the columns to these properties which works great on the initial load, however not so good when I want to change them. It works exactly as it's supposed to (changing the values on the base objects) for top level items, but for anything below that, it doesn't change the underlying values when I change the textbox value. I'm pretty new to WPF so sorry if this is basic but I have searched quite a while and can't find an answer that seems to work for me.
The XAML for the TreeListView is:
<r:TreeListView Name="TLV_Main" Margin="0,0,0,41" ItemsSource="{Binding Keynotes}" Style="{StaticResource TLV_Standard}">
<r:TreeListView.Columns>
<GridViewColumn Header="Key" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=KeyValue, Mode=TwoWay}" ContextMenu="{StaticResource Ctx_All}" ></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Description" Width="{Binding ElementName=TLV_Main, Path=ActualWidth, Converter={StaticResource DescriptionColumnConverter}}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Description, Mode=TwoWay}" TextWrapping="Wrap"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="30">
<GridViewColumn.Header>
<Image Source="pack://application:,,,/Resources/Icon_Comment.png" SnapsToDevicePixels="False" Stretch="None"></Image>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="pack://application:,,,/Resources/Icon_Comment.png" SnapsToDevicePixels="False" Stretch="None" Visibility="{Binding Converter={StaticResource BoolToVisibility}, Path=HasActiveComments}" MouseLeftButtonUp="CommentClick"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="30">
<GridViewColumn.Header>
<Image Source="pack://application:,,,/Resources/Icon_Link.png" SnapsToDevicePixels="False" Stretch="None"></Image>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="pack://application:,,,/Resources/Icon_Link.png" SnapsToDevicePixels="False" Stretch="None" Visibility="{Binding Converter={StaticResource BoolToVisibility}, Path=HasLinks}" MouseLeftButtonUp="LinkClick"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</r:TreeListView.Columns>
<r:TreeListView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildNotes}">
</HierarchicalDataTemplate>
</r:TreeListView.ItemTemplate>
</r:TreeListView>
Can anyone see why this wouldn't work right?

Resources