C# WPF Dynamic List with Binding - c#

I need to make a list , example : List
I get datas from SQL, it's possible to scroll if there are more datas.
I need to create this list in WPF.
I need to click on label 00000-0 or label "A" or "F".
I try this :
<ListView x:Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="3" Background="{x:Null}" ItemsSource="{Binding}" Foreground="White">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type local:CL_Operation}">
<Border BorderBrush="White" BorderThickness="0" CornerRadius="3" Margin="0,3" Grid.ColumnSpan="4" Background="Transparent">
<Border.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="5" />
</Border.Effect>
<Grid Background="Transparent" Margin="0,1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="1.5*"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical" Margin="50,5" >
<!-- <TextBlock Text="Customer" Foreground="#33B6EF" FontSize="20" />-->
<TextBlock Text="{Binding strPriorite}" FontSize="20" />
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1">
<!-- <TextBlock Text="Location" Foreground="#33B6EF" FontSize="20" />-->
<TextBlock Text="{Binding strRetardAvanceText}" FontSize="20" />
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="2">
<!-- <TextBlock Text="Created On" Foreground="#33B6EF" FontSize="20" />-->
<TextBlock Text="{Binding strNoOfOp}" FontSize="20" />
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="3">
<!-- <TextBlock Text="Quantity" Foreground="#33B6EF" FontSize="20" /> -->
<TextBlock Text="{Binding strDescriptionOp}" FontSize="20" />
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Window.Resources>
<DataTemplate DataType="{x:Type local:CL_Operation}" >
<Border BorderThickness = "1" BorderBrush = "Green" Margin = "2" Padding = "5" Width = "900" CornerRadius = "3" >
<Grid >
<Grid.ColumnDefinitions >
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions >
<TextBlock Grid.Column = "0" Text = "{Binding Path = strRetardAvanceText}" Background = "LightBlue" />
<!-- <TextBlock Grid.Column = "0" Text = "{Binding Path = strNoOfOp}" Background = "LightBlue" />-->
<TextBlock Grid.Column = "1" Text = "{Binding Path = strDescriptionOp}" Background = "LightBlue" />
<TextBlock Grid.Column = "2" Text = "{Binding Path = strNoArticle}" FontStyle = "Italic" />
</Grid >
</Border >
<DataTemplate.Triggers >
<DataTrigger Binding = "{Binding Path = strRetardAvanceText}" Value = "26 jrs" >
<Setter Property = "ListBoxItem.Foreground" Value = "Red" ></Setter >
</DataTrigger >
</DataTemplate.Triggers >
</DataTemplate >
</Window.Resources >
<Grid >
<ListBox x:Name = "listBox1" ItemsSource = "{Binding}" />
</Grid >
It's a good result but not on two columns, it's a lot of different
Thank you for your help.

Related

FlowDocument from XAML is not Responsive when Printing

The problem is that when I print the FlowDocument, it prints the same as what the Window is showing, and not the FlowDocument itself.
Here's the XAML:
<Window x:Class="POS.DailySalesReport"
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:POS"
mc:Ignorable="d"
Height="500"
Width="816"
Loaded="Window_Loaded"
WindowStartupLocation="CenterScreen">
<Grid>
<FlowDocumentPageViewer x:Name="flwView">
<FlowDocument x:Name="flwDoc"
TextAlignment="Center"
ColumnWidth="{Binding ElementName=flwView, Path=ActualWidth}">
<BlockUIContainer>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid x:Name="grdDetails"
Grid.Row="0"
Grid.Column="0"
Margin="0,0,0,30">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Ellipse Grid.Row="0"
Grid.Column="0"
Height="90"
Width="90"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Ellipse.Fill>
<ImageBrush x:Name="imgUser" />
</Ellipse.Fill>
</Ellipse>
<Grid Grid.Row="0"
Grid.Column="1"
Margin="10, 0, 0, 0">
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="lblCompanyName"
Grid.Row="0"
Grid.Column="0"
Text="COMPANY NAME"
FontFamily="{StaticResource RobotoRegular}"
FontSize="18"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock x:Name="lblCompanyAddress"
Grid.Row="1"
Grid.Column="0"
Text="Business Address"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock x:Name="lblCompanyNumber"
Grid.Row="2"
Grid.Column="0"
Text="Contact Number"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock x:Name="lblCompanyTIN"
Grid.Row="3"
Grid.Column="0"
Text="TIN Number"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
</Grid>
<StackPanel Grid.Row="0"
Grid.Column="2">
<TextBlock x:Name="lblReportType"
FontFamily="{StaticResource RobotoBold}"
FontSize="12"
HorizontalAlignment="Right" />
<TextBlock x:Name="lblReportDate"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
HorizontalAlignment="Right" />
</StackPanel>
</Grid>
<ListView x:Name="lstView"
Grid.Row="1"
Grid.Column="0"
BorderThickness="0"
SelectionMode="Single"
Width="{Binding ElementName=flwDoc, Path=ActualWidth}">
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White" />
</Setter.Value>
</Setter>
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="Black" />
</Setter.Value>
</Setter>
<Setter Property="FontSize"
Value="11" />
<Setter Property="FontFamily"
Value="{StaticResource RobotoMedium}" />
<Setter Property="Height"
Value="35" />
<Setter Property="BorderThickness"
Value="0, 1, 0, 1" />
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="Black" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="Black"
Opacity="0.8" />
</Setter.Value>
</Setter>
<Setter Property="FontFamily"
Value="{StaticResource RobotoMedium}" />
<Setter Property="FontSize"
Value="17" />
<Setter Property="Background"
Value="White" />
<Setter Property="Padding"
Value="0, 10, 0, 10" />
<Setter Property="Margin"
Value="0, 0, 0, 10" />
<Style.Resources>
<!-- Foreground for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black"
Opacity="0.8" />
<!-- Background for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="White" />
<!-- Foreground for Inactive Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black"
Opacity="0.8" />
<!-- Background for Inactive Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="White" />
</Style.Resources>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn x:Name="lstHeader1"
Header="Description">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductDescription}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader2"
Header="Supplier">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Supplier}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader3"
Header="Sold">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductSold}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader4"
Header="Cost Price">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductCost}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader5"
Header="Retail Price">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductRetail}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lstHeader6"
Header="Total(Qty * Retail Price)">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TotalRetail}"
TextAlignment="Center"
FontFamily="{StaticResource RobotoRegular}"
FontSize="12"
TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Grid Grid.Row="2"
Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0"
Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="Total Items Sold"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblTotalSold"
Grid.Row="0"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="Total Cost Amount"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblTotalCost"
Grid.Row="1"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
<TextBlock Grid.Row="2"
Grid.Column="0"
Text="Total Retail Amount"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblTotalRetail"
Grid.Row="2"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
<TextBlock Grid.Row="3"
Grid.Column="0"
Text="Profit"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoBold}" />
<TextBlock x:Name="lblProfit"
Grid.Row="3"
Grid.Column="1"
Text="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="10"
FontFamily="{StaticResource RobotoRegular}" />
</Grid>
</Grid>
</Grid>
</BlockUIContainer>
</FlowDocument>
</FlowDocumentPageViewer>
</Grid>
</Window>
Overall, the items in the ListView is dynamic. The problem appears when the list became longer than the height of the Window, ruining the appearance of the report (like the ellipse), and it is the same as the printed version.
Here's the code-behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
PrintDialog printDlg = new PrintDialog();
var actualWidth = lstView.ActualWidth - 10;
lstHeader1.Width = actualWidth / 6;
lstHeader2.Width = actualWidth / 6;
lstHeader3.Width = actualWidth / 6;
lstHeader4.Width = actualWidth / 6;
lstHeader5.Width = actualWidth / 6;
lstHeader6.Width = actualWidth / 6;
PopulateListView(ChosenDate);
string businessName = "";
string businessAddress = "";
string businessTIN = "";
string businessContact = "";
string businessImage = "";
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string query = "SELECT * FROM dbo.Company";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
try
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
businessName = reader["Name"].ToString();
businessAddress = reader["Address"].ToString();
businessTIN = reader["TIN"].ToString();
businessContact = reader["Contact"].ToString();
businessImage = reader["ImagePath"].ToString();
}
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message + err.StackTrace);
}
}
}
BitmapImage img;
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + businessImage))
{
img = new BitmapImage();
img.BeginInit();
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory + businessImage);
img.EndInit();
}
else
{
img = new BitmapImage(new Uri("pack://application:,,,/Resources/default_product_img.png"));
}
imgUser.ImageSource = img;
lblCompanyName.Text = businessName;
lblCompanyAddress.Text = businessAddress;
lblCompanyTIN.Text = businessTIN;
lblCompanyNumber.Text = businessContact;
lblReportType.Text = ReportType;
lblReportDate.Text = ChosenDate.ToString("MM/dd/yyyy");
lblTotalSold.Text = TotalSold.ToString();
lblTotalCost.Text = TotalCost.ToString("F2");
lblTotalRetail.Text = TotalRetail.ToString("F2");
lblProfit.Text = TotalProfit.ToString("F2");
IDocumentPaginatorSource idpSource = flwDoc;
var result = printDlg.ShowDialog();
if (result == true)
{
printDlg.PrintDocument(idpSource.DocumentPaginator, "Document");
}
}
I thought that the UI appearing in the Window is just for visual display, and not really what the PrintDialog prints, but the FlowDocument itself. If it is not possible, I'm planning to re-create the controls in code-behind just to maintain responsiveness. So I'm asking here first if there is a fix for this.

Re-Sort a ListView in C# - UWP

I've created a XAML UWP Listview
<ListView x:Name="ThisList"
Grid.Row="3"
Grid.ColumnSpan="7"
Background="LightBlue"
IsItemClickEnabled="True"
BorderBrush="Black"
BorderThickness="2"
ItemClick="ThisList_ItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid BorderThickness="1" BorderBrush="Black">
<Grid.Resources>
<local:ColourConverter x:Key="CCX"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="10*" MinWidth="500"/>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Border Background="Aquamarine" Grid.Column="0">
<SymbolIcon Symbol="{Binding Icon}" HorizontalAlignment="Center" Margin="10"/>
</Border>
<Border Background="Yellow" Grid.Column="1">
<TextBlock Name="tbFileName" Text="{Binding FName}" VerticalAlignment="Center" Margin="5" />
</Border>
<Border Background="Cyan" Grid.Column="2">
<!--<TextBlock Text="{Binding FTime}" VerticalAlignment="Center" Margin="5" />-->
<TextBlock Text="{Binding FTime}"
Name="FileTime"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Foreground="{Binding ElementName=FileTime,
Path=Text,
Mode=OneWay,
Converter={StaticResource CCX}}"/>
</Border>
<Border Background="Tomato" Grid.Column="3">
<TextBlock Text="{Binding FSize}" HorizontalAlignment="Right" Margin="10" />
</Border>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The code behind is
var folderItem = new ObservableCollection<FolderInfo>();
foreach (StorageFolder folder in folderList)
{
BasicProperties pro = await folder.GetBasicPropertiesAsync();
folderItem.Add(new FolderInfo()
{
FName = folder.DisplayName,
FSize = "",
FTime = lastFileSave,
FIsFolder = true
});
}
foreach (StorageFile file in fileList)
{
BasicProperties pro = await file.GetBasicPropertiesAsync();
lastFileSave = file.DateCreated.ToString(Globals.TimeFormat);
folderItem.Add(new FolderInfo()
{
//FName = file.DisplayName,
FName = file.Name,
FSize = GetFileSize(pro.Size),
FTime = lastFileSave,
FIsFolder = false,
Icon="Document"
});
}
showLoadingBox(false);
FileInfo.Text += $"Last file saved : {lastFileSave}";
ThisList.ItemsSource = folderItem;
So my question is that I'd like to create a header row that I can click on the title and it re-sorts the ListView by that column, and if I click again it re-sorts descending.
What is the best way to achieve this?
Ok so there was no rush of answers so I have created a separate grid to make the header, and added a tapped event to requery the data source, but to do a query on it as it is assigned to the listview source as follows.
ThisList.ItemsSource = (from FolderInfo in folderItem
orderby FolderInfo.RealDateTime
select FolderInfo);
It's probably not the most efficient way of doing things, but currently the only way I can see of doing it without the ListView having some kind of sort method available.
Would appreciate any comments if there is a better way of doing it.

Resize grid after rotation Windows phone 8.1

[![enter image description here][1]][1]I have problem with panel size in ListView. I have StackPanel in GridView and after rotation i want resize this gridview to the whole page, but after rotate stackPanel had the same widht as he had in portrait mode. Here is my code.
But when I start application on Landscape mode it is all ok and grid is resized.
<Page
x:Class="KlientWP.VypisZakazek"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:KlientWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded" SizeChanged="Page_SizeChanged">
<Page.Background>
<SolidColorBrush Color="{ThemeResource PhoneImagePlaceholderColor}"/>
</Page.Background>
<Grid x:Name="ContentRoot" Margin="0,9.5,0,0">
<ScrollViewer>
<Pivot Title="Přehled databáze" HorizontalAlignment="Stretch" Margin="0,0,0,0">
<PivotItem Header="Zakázky" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,0,20,10" >
<ListView SelectionMode="None" x:Name="ListBox1" Margin="0,0,-0.167,0.167"
HorizontalAlignment="Stretch"
ItemsSource="{Binding}" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid x:Name="grdTsk" Opacity="1" Margin="0,0,0,10" Width="1500" HorizontalAlignment="Stretch" Background="#FF302E2E" ManipulationMode="All" Tapped="grdTsk_Tapped" >
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Detail zakázky" Click="Detail" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Grid.Row="1" Text="{Binding nazev1}" Width="1500" FontSize="22" TextWrapping="Wrap" HorizontalAlignment="Stretch" Margin="5,0,0,0" Visibility="Visible" FontWeight="Light" Foreground="White"/>
<!--<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Margin="0,0,0,0" >
<TextBlock Text="{Binding nazev}" FontSize="22" TextWrapping="Wrap" Margin="5,0,0,0" Visibility="Visible" FontWeight="Light" Foreground="White"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<TextBlock x:Name="predmetTb" Text="{Binding zakazka}" FontSize="18" Margin="5,0,0,0" TextWrapping="Wrap" />
<TextBlock Text="{Binding kod_firmy}" FontSize="18" TextWrapping="Wrap" Margin="15,0,0,0" Foreground="#FFFFF413" />
</StackPanel>
</StackPanel>-->
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</PivotItem>
</Pivot>
</ScrollViewer>
</Grid>
binding:
CultureInfo culture = new CultureInfo("cs-CZ");
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<dataInfo>));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var obj = (List<dataInfo>)ser.ReadObject(stream);
List<dataInfo> VypisZakazekli = new List<dataInfo>();
VypisZakazekli.Clear();
foreach (dataInfo di in obj)
{
string iZakazka = "ID: " + di.zakazka;
string sNazev = di.nazev;
string sKod = "Firma: " + di.kod_firmy;
string sStatus = "Status: " + di.status_v;
string sDruh = di.druh_zakazky;
VypisZakazekli.Add(new dataInfo(iZakazka, sNazev, sKod, sStatus, sDruh));
}
this.ListBox1.ItemsSource = VypisZakazekli;
}
Try to add this to your listview :
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>

how to get listboxitem's value on listbox hold event?

xmal code:
<ListBox x:Name="listbox2" Margin="0,0" SelectionChanged="listbox2_SelectionChanged" Hold="listbox2_Hold" >
<ListBox.ItemContainerStyle >
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,1" BorderBrush="Gray">
<Grid Width="auto" HorizontalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" FontSize="40" Grid.Column="1" Grid.Row="0" Foreground="White" Text="{Binding NAME}"></TextBlock>
<TextBlock VerticalAlignment="Center" FontSize="25" Grid.Column="1" Grid.Row="1" Foreground="Blue" Text="{Binding PHONE}"></TextBlock>
<Image Name="c1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100" Stretch="Fill" Margin="0" Source="{Binding IMGS}" Grid.RowSpan="2" Grid.Column="0" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
class list which is bind to list box is
List < contactsclass > contacts = new List < contactsclass >();
PHONE and NAME are getter setter of the contactclass's variables
how can i get this variable's value on hold event of listbox .. i am trying following code
private void listbox2_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
//contextmenucontact = (contactsclass)(sender as ListBox).DataContext;
contextmenucontact = (contactsclass)listbox2.SelectedItem;
MessageBox.Show(contextmenucontact.name);
}
if is just the selected item is just use the ToString Function, see this:
if (listBox1.SelectedItem != null)
{
string itemText = listBox1.SelectedItem.ToString();
contextmenucontact = new contactsclass();
contextmenucontact.name = itemText;
MessageBox.Show(contextmenucontact.name);
}

Filter on Silverlight Datagrid using Radio Buttons C#

I have a datagrid:
<Grid x:Name="MainDataGrid" Grid.Row="5" >
<data:DataGrid x:Name="datagrid" AutoGenerateColumns="False" HorizontalAlignment="Stretch" IsReadOnly="True" VerticalAlignment="Top" ItemsSource="{Binding DataList}" Height="220" Margin="5">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="#" Binding="{Binding Number}" />
<data:DataGridTextColumn Header="Business Process" Width="300" Binding="{Binding BusinessProcess}" />
<data:DataGridTextColumn Header="Sub Process" Width="300" Binding="{Binding SubProcess}" />
<data:DataGridTextColumn Header="ITAC" Width="100" Binding="{Binding Itac}" />
<data:DataGridTextColumn Header="Status" Width="200" Binding="{Binding Status}" />
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
Which is currently bound to some dummy data I created in the ViewModel:
public class ItacViewModel : ViewModelBase
{
public List<data> DataList { get; set; }
public ItacViewModel()
{
var dataList = new List<data>
{
new data(1, "Fixed Asset", "Depreciation", "UL-AC1", "Under Review"),
new data(2, "Fixed Asset", "Depreciation", "UL-AC2", "Complete"),
new data(3, "Order to Cash", "Invoicing", "UL-AC3", "Under Review"),
new data(4, "Order to Cash", "Shipping Goods", "UL-AC4", "Under Review"),
new data(5, "Order to Cash", "Depreciation", "UL-AC5", "Complete"),
new data(6, "Order to Cash", "Depreciation", "UL-AC6", "Under Review"),
new data(7, "Order to Cash", "Depreciation", "UL-AC7", "Complete"),
new data(8, "Stock and Make", "Depreciation", "UL-AC8", "Under Review"),
new data(9, "Stock and Make", "Depreciation", "UL-AC9", "Under Review"),
new data(10, "General Accounting & Reporting", "Depreciation", "UL-AC10", "Complete")
};
DataList = dataList;
}
}
I wish to be able to filter the data when a Radio Button is selected (The Radio Button names match those in the second column in the datagrid):
<StackPanel Margin="2">
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="All Business Processes" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Fixed Asset" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Order to Cash" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Purchase to Pay" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Stock and Make" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="General Accounting and Reporting" FontStyle="Italic" />
</StackPanel>
<TextBlock Text=" " />
</StackPanel>
So far I have not been able to make this work.
EDIT
Here is all my code so far:
MAINPAGE.XAML
<UserControl x:Class="TabControl.MainPage"
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:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="800">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<controls:TabControl Margin="3">
<controls:TabItem Header="Local / Monitoring Controls" />
<controls:TabItem Header="IBM Controls" />
<controls:TabItem Header="ITAC's" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Border BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="ITAC's - IT Automated Controls in Key Financial Systems" FontSize="12" FontWeight="Bold" Margin="5" />
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Date Last Updated: " />
<TextBlock Text="Test Date, needs binding" />
</StackPanel>
</StackPanel>
</Border>
<Border BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2">
<TextBlock Text="ERP controls from Sirius" FontStyle="Italic" Margin="5" />
</Border>
</StackPanel>
<Grid x:Name="Controls" Grid.Row="1" Background="#A1AEC3">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Row="0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" Margin="1" >
<TextBlock Text="IT Automated Controls (Europe)" FontSize="12" FontWeight="Bold" Margin="5"/>
</Border>
<StackPanel Grid.Row="1" Grid.Column="0" Margin="0,0,10,0" >
<TextBlock Text="Total Active Controls" FontWeight="Bold" Margin="5,5,5,0" TextAlignment="Right" />
<TextBlock Text="ITAC's Under Review" Margin="5,5,5,0" TextAlignment="Right" />
<TextBlock Grid.Row="3" Grid.Column="0" Text="Outstanding Issues from Previous Months" Margin="5,5,5,0" TextAlignment="Right" />
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0" >
<TextBox Width="200" TextAlignment="Center" Text="33" />
<TextBox Width="200" TextAlignment="Center" Text="5" />
<TextBox Width="200" TextAlignment="Center" Text="0" />
</StackPanel>
</Grid>
<Grid x:Name="ITACs" Grid.Row="2" Background="#A1AEC3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.Column="0" BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" Margin="1" >
<StackPanel Orientation="Horizontal" Margin="3">
<StackPanel Orientation="Horizontal" >
<RadioButton GroupName="BusinessProcessTitle" />
<TextBlock Text="All ITAC's" FontWeight="Bold" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="100,0,0,0" >
<RadioButton GroupName="BusinessProcessTitle" />
<TextBlock Text="ITAC's Requiring Review" FontWeight="Bold" />
</StackPanel>
</StackPanel>
</Border>
<Border Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" BorderThickness="1" BorderBrush="WhiteSmoke" CornerRadius="2" Margin="1" >
<StackPanel Margin="2">
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="All Business Processes" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1" />
<TextBlock Text="Fixed Asset" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Order to Cash" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Purchase to Pay" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="Stock and Make" FontStyle="Italic" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="1">
<RadioButton GroupName="BusinessProcessesGroup" />
<TextBlock Text="General Accounting and Reporting" FontStyle="Italic" />
</StackPanel>
<TextBlock Text=" " />
</StackPanel>
</Border>
<Border Grid.Row="0" Grid.Column="1" BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" Margin="1" >
<Grid >
<TextBlock Text="ITAC Description" FontWeight="Bold" HorizontalAlignment="Left" Margin="3" />
<TextBlock Text="UL-AC1" FontStyle="Italic" HorizontalAlignment="Right" Margin="3"/>
</Grid>
</Border>
<Border CornerRadius="2" Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="WhiteSmoke">
<TextBlock TextWrapping="Wrap" Margin="3"
Text="Based on the asset class in the master data, the ERP system automatically calculates correct amounts for depreciation at the start of the depreciation run." />
</Border>
<Border CornerRadius="2" Grid.Row="2" Grid.Column="1" BorderThickness="1" BorderBrush="WhiteSmoke">
<StackPanel Orientation="Horizontal" Margin="1">
<TextBlock Text="Issue: " Margin="3,0,0,0" />
<TextBlock Text="Change Request to be created - Query Update Required" />
</StackPanel>
</Border>
</Grid>
<Grid x:Name="MainDataGrid" Grid.Row="5" >
<data:DataGrid x:Name="datagrid" AutoGenerateColumns="False" HorizontalAlignment="Stretch" IsReadOnly="True" VerticalAlignment="Top" ItemsSource="{Binding DataList}" Height="220" Margin="5">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="#" Binding="{Binding Number}" />
<data:DataGridTextColumn Header="Business Process" Width="300" Binding="{Binding BusinessProcess}" />
<data:DataGridTextColumn Header="Sub Process" Width="300" Binding="{Binding SubProcess}" />
<data:DataGridTextColumn Header="ITAC" Width="100" Binding="{Binding Itac}" />
<data:DataGridTextColumn Header="Status" Width="200" Binding="{Binding Status}" />
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</Grid>
</controls:TabItem>
<controls:TabItem Header="User Access" />
<controls:TabItem Header="User Provisioning" />
<controls:TabItem Header="About" />
</controls:TabControl>
</Grid>
</UserControl>
MAINPAGE.XAML.CS
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.DataContext = new ItacViewModel();
}
}
DATA.CS
public class data
{
public int Number { get; set; }
public string BusinessProcess { get; set; }
public string SubProcess { get; set; }
public string Itac { get; set; }
public string Status { get; set; }
public data(int number, string businessProcess, string subProcess, string itac, string status)
{
Number = number;
BusinessProcess = businessProcess;
SubProcess = subProcess;
Itac = itac;
Status = status;
}
}
ITACVIEWMODEL.CS
public class ItacViewModel : ViewModelBase
{
public List DataList { get; set; }
public ItacViewModel()
{
var dataList = new List<data>
{
new data(1, "Fixed Asset", "Depreciation", "UL-AC1", "Under Review"),
new data(2, "Fixed Asset", "Depreciation", "UL-AC2", "Complete"),
new data(3, "Order to Cash", "Invoicing", "UL-AC3", "Under Review"),
new data(4, "Order to Cash", "Shipping Goods", "UL-AC4", "Under Review"),
new data(5, "Order to Cash", "Depreciation", "UL-AC5", "Complete"),
new data(6, "Order to Cash", "Depreciation", "UL-AC6", "Under Review"),
new data(7, "Order to Cash", "Depreciation", "UL-AC7", "Complete"),
new data(8, "Stock and Make", "Depreciation", "UL-AC8", "Under Review"),
new data(9, "Stock and Make", "Depreciation", "UL-AC9", "Under Review"),
new data(10, "General Accounting & Reporting", "Depreciation", "UL-AC10", "Complete")
};
DataList = dataList;
}
}
Instead of binding your DataGrid to List<data> you should bind data to ICollectionView. ICollectionView makes operations like grouping, filtering etc very simple to implement on some Data.
Take a look at this simple yet informative article on how to achieve filtering in WPF through ICollectionView
Edit
As Silverlight doesn't support provide an implementation of ICollectionView out of the box take a look at this or this article
Here's the trivial way: modify the Visibility of your Datagrid columns depending on the state of your radiobuttons.
XAML:
<data:DataGridTextColumn x:Name="colFixedAsset" Header="Status" Width="200" Binding="{Binding Status}" />
<RadioButton x:Name="radioFixedAsset" GroupName="BusinessProcessesGroup" IsChecked="False" Checked="radioFixedAsset_Checked" Unchecked="radioFixedAsset_Unchecked" />
Code behind:
private void radioFixedAsset_Checked(object sender, RoutedEventArgs e)
{
this.colFixedAsset.Visibility = System.Windows.Visibility.Visible;
}
private void radioFixedAsset_Unchecked(object sender, RoutedEventArgs e)
{
this.colFixedAsset.Visibility = System.Windows.Visibility.Collapsed;
}
If you're a MVVM purist, then add boolean properties to your view model and bind the IsChecked properties of your radiobuttons to those booleans. Also bind the Visibility property of your datagrid columns to those booleans using a converter.

Categories

Resources