I am dynamically loading a xaml file. This is what the xaml looks like:
<ListView Grid.Row="2" BorderBrush="White"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="ListView1"
ItemsSource="{Binding Path=line}"
HorizontalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Lines"
DisplayMemberBinding="{Binding Path=aline}" />
</GridView>
</ListView.View>
</ListView >
The xaml cannot be changed.
You can just change the FontSize in your xaml.
<ListView Grid.Row="2" BorderBrush="White" FontSize="20" .................
However if you are loading your Xaml from a file you will have to load it first then change the FontSize
Example:
using (FileStream stream = new FileStream("c:\\test.xaml", FileMode.Open))
{
var listView = (ListView)XamlReader.Load(stream);
// change font size
listView.FontSize = 20;
// apply listView to whatever you need
}
Related
How to read data from a WPF ListView?
Here is my code.
<ListView x:Name="LVR" AllowDrop="True" PreviewDrop="LVR_PreviewDrop" RenderTransformOrigin="0.505,0.506" Margin="0,0,0,0" Grid.Row="1" Grid.ColumnSpan="3" MouseEnter="LVR_MouseEnter" >
<ListView.View>
<GridView >
<GridViewColumn Header="Status" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="index.png" Width="26"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="File Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TBtxt}" FontWeight="Bold" Foreground="Blue" Cursor="Hand" Height="30" TextAlignment="Left" HorizontalAlignment="Center"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
And i am inserting items to the List view like this.
void Insert()
{
WinForms.OpenFileDialog ofd = new WinForms.OpenFileDialog();
ofd.Multiselect = true;
ofd.Title = "Select .TXT File";
ofd.FileName = "";
ofd.Filter = "TXT | *.txt";
if (ofd.ShowDialog() == WinForms.DialogResult.OK)
{
foreach (var filename in ofd.FileNames)
{
if (System.IO.Path.GetExtension(filename).ToUpperInvariant() == ".txt")
{
LVR.Items.Add(new StackItems { TBtxt = filename });
}
}
}
}
class StackItems
{
public string TBtxt { get; set; }
public Image imgg { get; set; }
}
Once I completed adding files, my ListView will looks like this.
|Status | File Name|
|[Image]| test.txt |
|[Image]| test1.txt|
(Sorry. I don't have enough reputation to post image)
Now how will I read the 'File Name' from second column?
I am very new to WPF.
Thanks in advance.
In short, you should be data binding a collection of items (one for each row) to the ListView.ItemsSource property:
<ListView ItemsSource="{Binding SomeCollection}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>
If you do this, then accessing the items is as simple as this (using Linq):
var firstItem = SomeCollection.First();
An improvement on this situation would be to data bind another property of the same type as the objects on the data bound collection to the ListView.SelectedItem property:
<ListView ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding CurrentItem}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>
Doing this will enable you to access properties from the currently selected item from the ListView like this:
int someValue = CurrentItem.SomeProperty;
Please refer to the ListView Class page on MSDN for further help.
I have a 2 column ListView and I'm trying to fill it using and IDictionary with the following code.
System.Collections.IDictionary entryList = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
foreach (System.Collections.DictionaryEntry de in entryList)
{
Row row = new Row();
row.Name1 = (string)de.Key;
row.Name2 = (string)de.Value;
this.list1.Items.Add(row);
}
public class Row
{
public string Name1 { get; set; }
public string Name2 { get; set; }
}
XAML:
<ListView x:Name="varList"
Grid.ColumnSpan="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="400"
Width="500"
Margin="0, 30, 0, 0">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Name" />
<GridViewColumn Width="350" Header="Path" />
</GridView>
</ListView.View>
</ListView>
But every row and column gets filled with "Project.Views.Row".
Anyone got any idea on how to fix it? Thank you very much.
A ListView (and every other control for that matter) will display the results of calling ToString when given an object to display.
For a standard class, thats its qualified name; Project.Views.Row in your example.
There are two ways to fix this:
Don't add an object. Instead, format the string as you want it ie:
list1.Items.Add(String.Format({0}:{1}, row.Name1, row.Name2));
Do this the right way and use MVVM. In this case your XAML needs a data template:
<ListView ItemsSource="{Binding Rows}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name1}"/>
<TextBlock Text="{Binding Name2}"/>
</StackPanel>
</DataTemplate>
<ListView.ItemTemplate>
</ListView>
For a grid view:
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name1}" />
<GridViewColumn Header="Path" DisplayMemberBinding="{Binding Name2}"/>
</GridView>
</ListView.View>
Binding the ItemsSource is not actually necessary, but since we are doing everything the right way, you should do it so you are not directly manipulating the UI from code.
I am dynamically loading a xaml file into my program that has a binding:
<ListView
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Grid.Row="2" BorderBrush="White" Name="ListView1"
ItemsSource="{Binding Path=line}" HorizontalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Lines"
DisplayMemberBinding="{Binding Path=aline}" />
</GridView>
</ListView.View>
</ListView >
In my program, I want to check if the Binding exists.
How should this be achieved?
Edit: The aline is a property of the DataContext object
You can check for bindings like this:
BindingExpression be = BindingOperations.GetBindingExpression(ListView1, ItemsSourceProperty);
return be != null ? "ItemsSource is bound" : "ItemsSource is not bound";
if (ListView1.ItemsSource != null)
Console.WriteLine("Is Bound");
else Console.WriteLine("Is Not bound");
Sorry for the not so very general question...
I have a ListView that I have to fill from code behind, and this ListView also need to get its GridViewColumn's from code behind.
For strings it wasn't hard to make the connection, but now I wan't to create a Ellipse that represents a Boolean value in the ListView.
The code in XAML is rather easy, but I fail at converting it to c# code.
Here is parts of the XMAL code:
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="BoolToVisibility" />
<DataTemplate x:Key="templateAdmin">
<DockPanel>
<Ellipse Width="8" Height="8" Visibility="{Binding Path=isAdmin, Converter={StaticResource BoolToVisibility}}" Fill="Black"/>
</DockPanel>
</DataTemplate>
</ResourceDictionary>
<ListView>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="Last Name"/>
<GridViewColumn CellTemplate="{StaticResource templateAdmin}"
<GridViewColumnHeader">
<TextBlock Text="S"/>
</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
And by now I have gotten this far:
XAML:
<local:SortableListView >
<ListView.View>
<GridView x:Name="GroupListGridView" />
</ListView.View>
</local:SortableListView>
And in code I have a Collection<GridViewColumn> GridViewColumns that I loop throug and add all items to the GroupListGridView. And I have a function to fill the GridViewColumns collection:
private void CreateGridViews()
{
//Creating the Text was easy!
GridViewColumns.add(new GridViewColumn(){ Header = "LastName", DisplayMemberBinding = new Binding("LastName") });
//Creating the Ellipse was harder!
GridViewColumn gvc = new GridViewColumn();
DataTemplate dt = new DataTemplate();
gvc.DisplayMemberBinding = new Binding("isAdmin");
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Ellipse));
fef.SetValue(Ellipse.WidthProperty, 8.0D);
fef.SetValue(Ellipse.HeightProperty, 8.0D);
fef.SetValue(Ellipse.FillProperty, new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black));
//I'm guessing that somewhere here there should be some binding to the visibility property and some sort of conversion done... But I can't figure out how!
dt.VisualTree = fef;
gvc.CellTemplate = dt;
GridViewColumns.Add(gvc);
}
I don't think that I'm that far of... Just that I can't figure out those last steps!
The missing lines are:
var ellipseVisBinding = new Binding("isAdmin");
ellipseVisBinding.Converter = new BooleanToVisibilityConverter();
fef.SetBinding(Ellipse.VisibilityProperty, ellipseVisBinding);
(I note that you've excluded the DockPanel in the template from your code version so I've removed that as well)
I have the follwoing listview in my xaml:
<ListView Name="listView1">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Name"
DisplayMemberBinding="{Binding nombre}" />
<GridViewColumn Width="200" Header="LastName"
DisplayMemberBinding="{Binding razonSocial}" />
// etc....
I have an ObservableCollection binded to a listview. I created the binding behind code. So any changes that I make to that collection will be reflected on the listview. Also if I want to sort the listview I will just sort the ObservableCollection.
I sort the listview when the user clicks on a gridviewcolumnheader as:
listView1.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler((a, b) =>
{
// check to see what column header was clicked
string bindingProperty =
((Binding)(((GridViewColumnHeader)(b.OriginalSource)).Column.DisplayMemberBinding)).Path.Path;
// using dyniamic linq libraries or the example located at
// http://stackoverflow.com/a/233505/637142
// I will be able to sort my collection of objects by nowing the property name
}));
Anyways I will like to apply a diferent style to the GridViewColumnHeader that was just clicked. I believe there should be already an existing template.
I am looking for something like:
GridViewColumnHeader a = "gridviewColumnHeader that was clicked"
a.Style = "orderByAscGridViewColumnTemplate"
Code Behind:
listView1.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler((a, e) =>
{
GridViewColumnHeader headerClicked =
e.OriginalSource as GridViewColumnHeader;
headerClicked.Column.HeaderTemplate =
Resources["HeaderTemplateArrowUp"] as DataTemplate;
xaml:
<UserControl.Resources>
<DataTemplate x:Key="HeaderTemplateArrowUp">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="{Binding}"/>
<Path x:Name="arrow"
StrokeThickness = "1"
Fill = "gray"
Data = "M 5,10 L 15,10 L 10,5 L 5,10"/>
</DockPanel>
</DataTemplate>
</UserControl.Resources>