I have user control with a fixed page element, everything works fine from the UI but when I print it the data in the listview is not printed on the receipt I tried several things but no matter what it just doesn't show up.
Here is the XAML
<FixedPage x:Name="ReceiptData">
<StackPanel Orientation="Vertical">
<Image Source="../Images/LOGO.png" Width="340" Margin="5,1,1,1"></Image>
<StackPanel Orientation="Horizontal" >
<Label Margin="5,1,0,1" Content="{Binding KioskName, FallbackValue=000000}"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Date:" Margin="5,1,8,1"></Label>
<Label Content="{Binding CurrentDateTime, StringFormat=g}"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Border BorderThickness="0,1,0,0"
BorderBrush="Black"
Height="5" Width="345"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Qauntity"></Label>
<Label Content="Description" Margin="8,0,0,0"></Label>
<Label Content="Price" Margin="130,0,0,0"></Label>
</StackPanel>
<ListView ItemsSource="{Binding SaleItemsObservable}" MinHeight="300"
BorderBrush="{x:Null}"
Background="{x:Null}"
Foreground="Black">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource gridViewNoHeader}">
<GridViewColumn DisplayMemberBinding="{Binding Quantity}" Width="45"/>
<GridViewColumn DisplayMemberBinding="{Binding ProductName}" Width="210" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat='{}{0:c}'}" Width="55"/>
</GridView>
</ListView.View>
</ListView>
<Image Source="../Images/LOGO.png" Width="340" Margin="5,1,1,1"></Image>
</StackPanel>
</FixedPage>
This is the code that prints it.
private void Button_Click(object sender, RoutedEventArgs e)
{
InnerGrid.Children.Remove(ReceiptData);
PrintQueue sourcePrintQueue = new LocalPrintServer().DefaultPrintQueue;
if (sourcePrintQueue != null)
{
var pageContent = new PageContent();
var fixedDocument = new FixedDocument();
fixedDocument.PrintTicket = new PrintTicket();
var printTicket = (PrintTicket)fixedDocument.PrintTicket;
printTicket.PageOrientation = PageOrientation.Portrait;
((IAddChild)pageContent).AddChild(ReceiptData);
fixedDocument.Pages.Add(pageContent);
PrintDialog pd = new PrintDialog();
pd.PrintQueue = sourcePrintQueue;
pd.PrintTicket = printTicket;
pd.PrintDocument(fixedDocument.DocumentPaginator, "Kiosk 2.0 Receipt");
}
}
It prints perfectly fine but the data in the listview is missing no matter what, I have tried setting a template, I have tried manually setting the items in the code behind but nothing works so I assume there is something I am missing to tell it to render that data to the printer? Any ideas?
Related
I've seen plenty of examples of how to add a double-click event for each ListView item, but I can't figure out how to implement it to my code. The best example I found so far is this one in MS docs, but it still doesn't help me handle this :
<ListView Name="listViewItem" ClipToBounds="True" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" SizeChanged="ListView_SizeChanged" TextOptions.TextHintingMode="Animated" Margin="0,0,117,0">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Task ID" Width="0">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskID}" TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Tast Title" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskTitle}" TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Task Deadline" Width="275">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskDeadline}" TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Task Group" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskGroup}" TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Task Contact" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskContact}" TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Task Workers" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskWorkers}" TextWrapping="Wrap" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I have a helpers class that sets values on load event ->
public void GetGridTasks(ref ListView listViewItem)
{
SqlCommand sqlCommandRefresh = new SqlCommand("", dataconnection);
sqlCommandRefresh.CommandText = "SELECT TaskID, TaskTitle, TaskDeadline, TaskGroup, TaskContact, TaskWorkers FROM Tasks";
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommandRefresh);
DataTable dt = new DataTable("Tasks");
sqlAdapter.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
dr["TaskWorkers"] = dr["TaskWorkers"].ToString().Replace("||", ", ");
}
listViewItem.ItemsSource = dt.DefaultView;
}
This is basically TextBlocks inside GridView inside ListView. Can't really wrap my head around fixing this mess.
Just use the MouseDoubleClick Event from the ListView.
Example:
<ListView MouseDoubleClick="ListView_MouseDoubleClick">
//Items here
</ListView>
In your code behind you simply add an event handler for that
void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e){
var item = ((FrameworkElement) e.OriginalSource).DataContext
var myItem = item as *CastToWhateverTypeYouNeed*
if (item != null){
//Here you have your item
}
}
I want to get all the items in a listview that the checkbox has checked.
<ListView x:Name="lvwStudent" IsSynchronizedWithCurrentItem="True" SelectionMode="Multiple" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
<ListView.View>
<GridView>
<GridViewColumn Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="cboxSelected" Content ="{Binding ID}" Width="20" Height="20" BorderBrush="#FF0C6161" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Tag="{Binding ID}" IsChecked="{Binding IsChecked}" Checked="cboxSelected_Checked_1" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="NO." Width="53">
<GridViewColumn.CellTemplate>
<DataTemplate >
<Label Content ="{Binding ID}" FontSize="14" HorizontalContentAlignment="Center"></Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="STUDENT NAME" Width="auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content ="{Binding STUDENT_NAME}" FontSize="14" HorizontalContentAlignment="Center"></Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
when I click the select button I want to get all selected ID and store in an array.
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
int[] selectedId;
if(lvwMachine.SelectedItems.Count > 0)
{
foreach(..... )
{
//add all selected id in array selectedId
}
}
List<Student>StudentList = new Students().getStudent();
List<ListViewItem> ITEM = new List<ListViewItem>();
foreach (var s in StudentList)
{
ListViewItem OneItem = new ListViewItem();
OneItem.Content = new Student()
{ID = s.ID, STUDENT_NAME = s.name};
ITEM.Add(OneItem);
lvwMachine.ItemsSource = ITEM;
}
You can try this:
lvwMachine.ItemsSource.Where(element => element.IsChecked).ToArray();
I'm trying to bind a list of my class into a ListView. I've tried many issues.
There is my c# Code witch show how I define:
public MainWindow()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(1); // L’intervalle entre chaque tick du timer sera d’une seconde
timer.Tick += new EventHandler(timer_Tick); // A chaque tick, on déclenche l’évènement timer_Tick
MyMP3LIST = new List<ListGrid>();
ListM.DataContext = MyMP3LIST; // ListM = ListView name
// I've tried : ListM.ItemSource = MyMP3LIST;
}
When try to bind :
ListGrid l = new ListGrid(); // My Class
l.IconUri = imagemp3.Source;
l.Title = Ftitle;
l.Length = duration;
l.Album = Falbum;
l.Composer = Fcomposer;
l.Path = openFileDialog1.FileName;
MyMP3LIST.Add(l);
My XAML:
<ListView x:Name="ListM" Width="Auto" ItemsSource="{Binding MyMP3LIST}"
Margin="-3,-0.877,-4,-15.925" SelectionChanged="ListM_SelectionChanged">
Just the first imported file is display into the ListView.
After populating MyMP3LIST Change this:
ListM.DataContext = MyMP3LIST;
To this:
this.DataContext = MyMP3LIST;
And also change this:
<ListView x:Name="ListM" Width="Auto" ItemsSource="{Binding MyMP3LIST}" ../>
To this:
<ListView x:Name="ListM" Width="Auto" ItemsSource="{Binding}" .../>
Also in your ListView you need ItemTemplate to show data. For example:
<ListView x:Name="ListM" Width="Auto" ItemsSource="{Binding}" ...>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Album}" />
...
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView>
Edit: Based on your comments that you said you want to add item with an OpenFileDialog and you want to update your ListView, MyMP3LIST should be ObservableCollection:
ObservableCollection<ListGrid> MyMP3LIST = new ObservableCollection<ListGrid>();
Sorry for the double answer but comments do not allow code.
This is just a sample so I'm not sure if all the properties are exact, but it should look like this:
<ListView Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=YourCollection, Mode=OneWay}"
SelectedItem="{Binding Path=YourSelectedItem, Mode=TwoWay}"
Margin="0"
>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<WrapPanel>
<TextBlock Text="{Binding Path=FieldName1}" />
<TextBlock Text="{Binding Path=FieldName2}" />
</WrapPanel>
<WrapPanel>
<TextBlock Text="{Binding Path=FieldName3}" />
<TextBlock Text="{Binding Path=FieldName4}" />
</WrapPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The Item template can be defined with any kind of WPF control you like for your properties.
I am new to c# WPF app developing, currently I write my first application and I was able to display a list on a window but only in one box.
Fitness box works perfectly but I can't get second one to work. If I change the order and put WeightList first then it works and ActivityList is not displayed. Here is the code:
XAML:
<Grid>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="178" VerticalAlignment="Top" Width="220" Margin="0,3,0,0">
<Grid>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" Height="20" VerticalAlignment="Top"><Run Text="FITNESS"/></TextBlock>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="88*"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<ListView HorizontalAlignment="Left"
Height="150" Margin="0,26,0,0" VerticalAlignment="Top" Width="218"
x:Name="ActivityList" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Type"
DisplayMemberBinding="{Binding Type}"/>
<GridViewColumn Header="Date"
DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Distance"
DisplayMemberBinding="{Binding Distance}"/>
</GridView>
</ListView.View>
</ListView>
<Button Margin="8,0,0,156" Grid.Column="1" Width="20" Height="20" Click="Button_Click" Opacity="0.8">
<Grid HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Source="Images/Button_11-512.png" Width="16" Margin="-15,-4,-1,-5" />
</Grid>
</Button>
</Grid>
</Grid>
</Border>
<Expander Header="NOTES" HorizontalAlignment="Left" Margin="0,181,0,0" VerticalAlignment="Top" Width="220" Height="75">
<StackPanel Margin="10,4,0,0">
<TextBox Margin="4" />
</StackPanel>
</Expander>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="178" VerticalAlignment="Top" Width="220" Margin="238,3,0,0">
<Grid>
<TextBlock TextWrapping="Wrap" TextAlignment="Center" Height="20" VerticalAlignment="Top"><Run Text="WEIGHT CONTROL"/></TextBlock>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="88*"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<ListView HorizontalAlignment="Left"
Height="150" Margin="0,26,0,0" VerticalAlignment="Top" Width="218"
x:Name="WeightList" Grid.ColumnSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Date"
DisplayMemberBinding="{Binding Date}"/>
<GridViewColumn Header="Weight"
DisplayMemberBinding="{Binding currentWeight}"/>
</GridView>
</ListView.View>
</ListView>
<Button Margin="8,0,0,156" Grid.Column="1" Width="20" Height="20" Opacity="0.8">
<Grid HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Source="Images/Button_11-512.png" Width="16" Margin="-15,-4,-1,-5" />
</Grid>
</Button>
</Grid>
</Grid>
</Border>
<Expander Header="NOTES" HorizontalAlignment="Left" Margin="238,181,0,0" VerticalAlignment="Top" Width="220" Height="75">
<StackPanel Margin="10,4,0,0">
<TextBox Margin="4" />
</StackPanel>
</Expander>
</Grid>
MainWindow.cs
public partial class MainWindow : ModernWindow
{
public static ObservableCollection<Activity> Activities;
public static ObservableCollection<Weight> WeightControl;
public MainWindow()
{
InitializeComponent();
Activities = new ObservableCollection<Activity>() {
new Activity() {Type = "Running", Date = "15.07.2015", Distance = "5km"},
new Activity() {Type = "Cycling", Date = "17.07.2015", Distance = "120km"},
new Activity() {Type = "Swimming", Date = "19.07.2015", Distance = "3km"},
};
ActivityList.ItemsSource = Activities;
WeightControl = new ObservableCollection<Weight>() {
new Weight() {Date = "15.07.2015", currentWeight = 61.2},
new Weight() {Date = "17.07.2015", currentWeight = 62.1},
new Weight() {Date = "19.07.2015", currentWeight = 61.9},
};
Image with problem
You forgot to set WeightList.ItemsSource. this should do it:
WeightList.ItemsSource = WeightControl;
I'm using this RSS feed for my Windows 8 Application (c#) http://www.skysports.com/rss/0,20514,11661,00. I can display the Title and PubDate but I'm stuck on the image.
private async void LoadRSS()
{
SyndicationClient client = new SyndicationClient();
Uri feedUri = new Uri("http://www.skysports.com/rss/0,20514,11661,00.xml");
SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
FeedData feedData = new FeedData();
foreach (SyndicationItem item in feed.Items)
{
FeedItem feedItem = new FeedItem();
feedItem.Title = item.Title.Text;
feedItem.PubDate = item.PublishedDate.DateTime;
// Handle the differences between RSS and Atom feeds.
if (feed.SourceFormat == SyndicationFormat.Atom10)
{
feedItem.Content = item.Content.Text;
feedItem.Link = new Uri("http://www.skysports.com" + item.Id);
}
else if (feed.SourceFormat == SyndicationFormat.Rss20)
{
feedItem.Content = item.Summary.Text;
feedItem.Link = item.Links[0].Uri;
}
feedData.Items.Add(feedItem);
}
ItemListView.DataContext = feedData.Items;
}
xaml code:
<GridView x:Name="ItemListView" Grid.Column="2" Grid.Row="1" ItemsSource="{Binding}" ItemClick="Sports_ItemClick_1" IsItemClickEnabled="True" SelectionMode="None" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid Width="400" Height="75" Margin="0,0,50,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition Width="325"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="#60BF89">
<Image Source="{Binding ImagePath, Mode=OneWay}" Stretch="None" Margin="5,15,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" Width="500"/>
</Border>
<StackPanel Grid.Column="1" VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Foreground="White" Text="{Binding Title}" FontSize="16" Margin="5,0,0,0" TextWrapping="Wrap" />
<TextBlock Foreground="White" Text="{Binding PubDate}" FontSize="12" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
I would like the Image in the first column. Any ideas??
you are not setting ImagePath property in the code sample you showed, considering this as a problem