WPF throws System.NullReferenceException when trying to modify xaml elements - c#

Ive been trying to make a Germany excersice program (trying to learn it but its hard) but I ran into "System.NullReferenceException" error. Any idea what is causing that AND how to fix it ? Thanks :)
I have been looking for a way to change multiple (up to 20 in this case) objects parameter to a certain value with not HUUUGE code and crazy ways about it because I am pretty new programmer and still dont know all the shortcuts and stuff :D
I browsed alot on the internet trying to find a way to do it but I was not succesfull so I am asking here. Sorry if its alot of code I think its better to post all then miss something that doesnt seem important to me but actually is important right ? :)
Oh and I encountered the exception in the "((("clen" + x) as object) as TextBox).IsEnabled = true;" part. But it think it would also happen at any other place in the code where i am using similiar as-as part
XAML:
<Window x:Class="NJ.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:NJ"
mc:Ignorable="d"
Title="MainWindow" Height="900" Width="1600">
<Grid>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" Name="clen1" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" Name="slovicko1" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen2" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko2" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen3" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko3" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="40" Margin="10,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen4" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="427" x:Name="slovicko4" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen5" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko5" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen6" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko6" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen7" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko7" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="40" Margin="10,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen8" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="427" x:Name="slovicko8" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="40" Margin="10,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen9" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="427" x:Name="slovicko9" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="10,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen10" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="106,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="427" x:Name="slovicko10" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,59,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen11" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko11" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen12" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,110,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko12" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen13" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,161,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko13" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="40" Margin="651,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen14" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,212,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="428" x:Name="slovicko14" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen15" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,264,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko15" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen16" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko16" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen17" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,366,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko17" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="40" Margin="651,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen18" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,417,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="428" x:Name="slovicko18" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="40" Margin="651,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen19" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,468,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="428" x:Name="slovicko19" IsEnabled="False" FontSize="25"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="651,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="76" x:Name="clen20" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="746,520,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="428" x:Name="slovicko20" IsEnabled="False" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="1266,33,0,0" TextWrapping="Wrap" Text="Od lekce:" VerticalAlignment="Top" Height="39" Width="90" FontSize="20"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="1362,31,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="38" Name="zacateklekci" FontSize="25"/>
<TextBlock HorizontalAlignment="Left" Margin="1426,33,0,0" TextWrapping="Wrap" Text="do lekce:" VerticalAlignment="Top" Height="39" Width="90" FontSize="20"/>
<TextBox HorizontalAlignment="Left" Height="39" Margin="1520,31,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="38" Name="koneclekci" FontSize="25"/>
<Slider HorizontalAlignment="Left" Margin="1290,110,0,0" VerticalAlignment="Top" Maximum="20" Height="39" Width="250" TickPlacement="BottomRight" TickFrequency="1" IsSnapToTickEnabled="True" Name="wordcount" Minimum="1" Value="1"/>
<TextBlock HorizontalAlignment="Left" Margin="1236,109,0,0" TextWrapping="Wrap" Text="{Binding ElementName=wordcount, Path=Value, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Height="40" Width="49" FontSize="25"/>
<Button Content="Nový test" HorizontalAlignment="Left" Margin="1266,161,0,0" VerticalAlignment="Top" Width="274" Height="39" FontSize="25" Click="Button_Click" Name="generateTest"/>
<Button Content="Zkontrolovat" HorizontalAlignment="Left" Margin="1266,264,0,0" VerticalAlignment="Top" Width="274" Height="39" FontSize="25" Click="kontroluj" Name="zkontolujbutton"/>
<TextBlock HorizontalAlignment="Left" Margin="1266,315,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="39" Width="274" FontSize="25" Name="pocetchyb"/>
</Grid>
C# backcode
public partial class MainWindow : Window
{
public int chyby = 0;
public List<string> comparelist = new List<string>();
public List<string> derlist = new List<string>();
public List<string> dielist = new List<string>();
public List<string> daslist = new List<string>();
public List<string> blacklist = new List<string>();
int tmp1 = 1;
int tmp2 = 1;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Int32.TryParse(zacateklekci.Text, out tmp1) && Int32.TryParse(koneclekci.Text, out tmp2))
{
if (tmp1 < 8 && tmp1 > 0 && tmp2 < 8 && tmp2 > 0)
{
chyby = 0;
for (int i = 1; i <= tmp2; i++)
{
using (StreamReader r = new StreamReader(#"C:\Users\Honza\Documents\VS Files\nj\data" + i + "der.json"))
{
string json = r.ReadToEnd();
foreach (string s in JsonConvert.DeserializeObject<List<String>>(json)) derlist.Add(s);
}
using (StreamReader r = new StreamReader(#"C:\Users\Honza\Documents\VS Files\nj\data" + i + "die.json"))
{
string json = r.ReadToEnd();
foreach (string s in JsonConvert.DeserializeObject<List<String>>(json)) dielist.Add(s);
}
using (StreamReader r = new StreamReader(#"C:\Users\Honza\Documents\VS Files\nj\data" + i + "das.json"))
{
string json = r.ReadToEnd();
foreach (string s in JsonConvert.DeserializeObject<List<String>>(json)) daslist.Add(s);
}
}
}
}
Random rand = new Random();
for (int x = 1; x <= (wordcount.Value + 1); x++)
{
((("clen" + x) as object) as TextBox).IsEnabled = true;
((("slovicko" + x) as object) as TextBlock).IsEnabled = true;
int tmp = 0;
switch (rand.Next(3))
{
case 1:
comparelist.Add("die");
do
{
tmp = rand.Next((dielist.Count + 1));
} while (blacklist.Contains(dielist[tmp]));
((("slovicko" + x) as object) as TextBlock).Text = dielist[tmp];
blacklist.Add(dielist[tmp]);
break;
case 2:
comparelist.Add("das");
do
{
tmp = rand.Next((daslist.Count + 1));
} while (blacklist.Contains(daslist[tmp]));
((("slovicko" + x) as object) as TextBlock).Text = daslist[tmp];
blacklist.Add(daslist[tmp]);
break;
default:
comparelist.Add("der");
do
{
tmp = rand.Next((derlist.Count + 1));
} while (blacklist.Contains(derlist[tmp]));
((("slovicko" + x) as object) as TextBlock).Text = derlist[tmp];
blacklist.Add(derlist[tmp]);
break;
}
}
generateTest.IsEnabled = false;
zkontolujbutton.IsEnabled = true;
}
public void ClearComparsion()
{
comparelist = new List<string>();
derlist = new List<string>();
dielist = new List<string>();
daslist = new List<string>();
blacklist = new List<string>();
try
{
for (int i = 1; i < 21; i++)
{
((("clen" + i) as object) as TextBox).IsEnabled = false;
((("clen" + i) as object) as TextBox).Background = Brushes.Transparent;
((("clen" + i) as object) as TextBox).Text = null;
((("slovicko" + i) as object) as TextBlock).IsEnabled = false;
((("slovicko" + i) as object) as TextBlock).Background = Brushes.Transparent;
((("slovicko" + i) as object) as TextBlock).Text = null;
}
} catch (Exception e) {}
generateTest.IsEnabled = true;
zkontolujbutton.IsEnabled = false;
}
private void kontroluj(object sender, RoutedEventArgs e)
{
for (int i = 1; i <= (wordcount.Value + 1); i++)
{
if (((("clen" + i) as object) as TextBox).Text.ToLower().Equals(comparelist[i]))
{
((("clen" + i) as object) as TextBox).Background = Brushes.LightGreen;
} else
{
((("clen" + i) as object) as TextBox).Background = Brushes.Pink;
chyby++;
}
}
ClearComparsion();
}
}

The problem is with your logic to find/access controls, for example ((("clen" + x) as object) as TextBox) and similar code where you cast object as a TextBox.
when you say ("clen" + x) as object is essentially a String object and not a TextBox object. So when you cast the String object to TextBox, it will result in a null.
To fix the issue, you can use FindName method which will search for all elements for a given name on the window. Mind that you cannot use it to search for elements in side templates, which you are not doing. So the fixed code will look like:
(FindName("clen" + x) as TextBox)
Make sure to check for null result.

Related

Need to get my enum inserted into a combobox in C#

I need help with getting the information in my enum into a combobox.
Here is the code for my enum:
namespace Arsalan_Salam_991571527_A2
{
public enum CarType
{
Odyssey,
Rouge,
Sienna,
Accord
}
}
I have found some code that was suppose to work and I tried to implement into my code to make the information inside of enum appear as shown below:
private void AddingEnumIntoComboBox(Car c)
{
foreach (var item in Enum.GetValues(typeof(CarType)))
{
carTypeInput.Items.Add(item);
}
}
But for some reason the program works fine but this code does not show the information of my enum into the combobox which is called carTypeInput. This is for a college assignment.
Here is the xaml that I used to create the UI interface:
<Page
x:Class="Arsalan_Salam_991571527_A2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Arsalan_Salam_991571527_A2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid RenderTransformOrigin="0.497,0.522">
<TextBlock HorizontalAlignment="Left" Margin="572,10,0,0" Text="DriveWell Inc." TextAlignment="Center" FontSize="50" VerticalAlignment="Top" Width="374" Height="72"/>
<TextBlock HorizontalAlignment="Left" Margin="87,88,0,0" Text="Vin Number" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="192" Height="67" RenderTransformOrigin="0.457,-0.751"/>
<TextBlock HorizontalAlignment="Left" Margin="67,185,0,0" Text="Car Make" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="194" Height="63"/>
<TextBlock HorizontalAlignment="Left" Margin="87,282,0,0" Text="Car Type" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="183" Height="61"/>
<TextBlock HorizontalAlignment="Left" Text="Purchase Price" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Margin="87,380,0,0" Width="226" Height="61" RenderTransformOrigin="3.948,-0.233"/>
<TextBlock HorizontalAlignment="Left" Margin="87,487,0,0" Text="Model Year" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Height="65" Width="190" RenderTransformOrigin="3.283,-2.555"/>
<TextBlock HorizontalAlignment="Left" Margin="87,584,0,0" Text="Mileage (Km)" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Height="43" Width="192"/>
<Button x:Name="addingCar" Click="addingCar_Click" Content="Add Car" FontSize="30" Margin="43,639,0,0" VerticalAlignment="Top" Height="56" Width="156"/>
<Button x:Name="clearing" Click="clearing_Click" Content="Clear" FontSize="30" Margin="224,639,0,0" VerticalAlignment="Top" Height="56" Width="134"/>
<Button x:Name="updatingCar" Click="updatingCar_Click" Content="Update" FontSize="30" Margin="379,639,0,0" VerticalAlignment="Top" Height="56" Width="130"/>
<ComboBox x:Name="carTypeInput" Margin="348,282,0,0" Width="191" Height="57"/>
<ComboBox x:Name="modelYearInput" Margin="348,483,0,0" Width="191" Height="52"/>
<TextBox x:Name="vinNumberInput" HorizontalAlignment="Left" Margin="348,88,0,0" Text="" FontSize="25" VerticalAlignment="Top" Height="40" Width="191" RenderTransformOrigin="0.476,-1.383"/>
<TextBox x:Name="carMakeInput" HorizontalAlignment="Left" Margin="348,185,0,0" Text="" FontSize="25" VerticalAlignment="Top" Height="40" Width="191"/>
<TextBox x:Name="purchasePriceInput" HorizontalAlignment="Left" Margin="348,380,0,0" Text="" FontSize="25" VerticalAlignment="Top" Height="52" Width="191"/>
<TextBox x:Name="mileageInput" HorizontalAlignment="Left" Margin="348,584,0,0" Text="" FontSize="15" VerticalAlignment="Top" Height="32" Width="191"/>
<Image x:Name="carImageOutput" HorizontalAlignment="Left" Height="429" Margin="1013,106,0,0" VerticalAlignment="Top" Width="226"/>
<TextBlock x:Name="errorMessageOutput" HorizontalAlignment="Left" Margin="572,624,0,0" Text="" FontSize="35" VerticalAlignment="Top" Width="641" Height="62"/>
<ListView x:Name="lstCarDetailOutput" Margin="572,88,315,120"></ListView>
</Grid>
</Page>
jdweng pointed this out in their comment, but I'll expand on it and make it an answer:
The problem is that Enum.GetValues returns the values of an enum, which is a integral type (currently C# enums are more or less a fancy wrapper around a bunch of constant numbers). By default this is a int (more here). Meaning your call to Enum.GetValues(typeof(CarType)) is returning int[]. Now there are multiple ways to get the name of an enum value, I'll demonstrate two.
Convert the int back to an enum value and call ToString
foreach (var item in Enum.GetValues(typeof(CarType))
{
// This can be written as 'carTypeInput.Items.Add(((CarType) item).ToString());'
var carType = (CarType) item;
carTypeInput.Items.Add(carType.ToString());
}
Use Enum.GetName to avoid having to get an instance of CarType
foreach (var item in Enum.GetValue(typeof(CarType))
{
// This can be written as 'carTypeInput.Items.Add(Enum.GetName(typeof(CarType), item));
var carTypeName = Enum.GetName(typeof(CarType), item);
carTypeInput.Items.Add(carTypeName);
}
Why not use Enum.GetNames?
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(Enum.GetNames(typeof(CarType)));
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string carTypeName = comboBox1.SelectedItem.ToString();
if(carTypeName == CarType.Accord.ToString())
{
...
}
}

MVVM Methodology printing text content from textboxes in a usercontrol window

I have just started to learn the MVVM methodolgy. I have written a simple application that basically is a application form. My UserControl window is a bunch of labels and textboxes. I bind the textboxes to elements in an Application class. The user can either choose to load the data from a Google spreadsheet or hard code the data. This all works great. I also want to be able to print the form with the values in the textboxes. The window layout prints just fine but the value in the textboxes. I am just doing a printobjext.printvisual command. My question is how do I print the contents of the textboxes, do I need to bind the data in some other way?
Here is part of my UserControl XAML window. just eliminating redundent code.
<UserControl x:Class="ApplicationForm.Views.ApplicationView"
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:ApplicationForm"
xmlns:model="clr-namespace:ApplicationForm.Model"
xmlns:views="clr-namespace:ApplicationForm.Views"
xmlns:viewModel="clr-namespace:ApplicationForm.ViewModel"
mc:Ignorable="d"
Height="980" Width="800" Background="#FFDAFDF2">
<!--<UserControl.Resources>
<ResourceDictionary>
<viewModel:ApplicationViewModel x:Key="AppViewModel"/>
</ResourceDictionary>
</UserControl.Resources>-->
<Grid x:Name="grdAppForm">
<Grid x:Name="grdAppFormGrid" DocumentViewerBase.IsMasterPage="True" VirtualizingPanel.VirtualizationMode="Standard"
VirtualizingPanel.IsVirtualizingWhenGrouping="True" HorizontalAlignment="Center" VerticalAlignment="Center" Height="820" Width="700">
<Label x:Name="lblName" Content="Name:" Height="28" Width="58" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
<TextBox x:Name="tbxName" DataContext="{Binding Applicants, Mode=TwoWay}" Text="{Binding FullName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, BindsDirectlyToSource=True}"
Height="22" Width="215" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="83,10,0,0" FontFamily="Arial" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" FontSize="16" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblFamilySize" Content="Family Size:" Height="28" Width="102" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="362,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
<Label x:Name="lblFamO" Content="O:" Height="28" Width="27" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="461,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="txbFamO" DataContext="{Binding Applicants, Mode=TwoWay}" Text="{Binding Path=FamilyO, Mode=TwoWay, BindsDirectlyToSource=True}" Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="486,10,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblFamA" Content="A:" Height="28" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="506,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="tbxFamA" DataContext="{Binding Applicants}" Text="{Binding Path=FamilyA, Mode=TwoWay}" Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="531,10,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblFamC" Content="C:" Height="28" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="552,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="txtFamC" DataContext="{Binding Applicants}" Text="{Binding Path=FamilyC, Mode=TwoWay}" Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="577,10,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblEqual" Content="=" Height="28" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="601,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
<TextBox x:Name="tbxFamTot" DataContext="{Binding Applicants}" Text="{Binding Path=FamilyTotal,
Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Height="22" Width="30"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="624,10,0,0" FontFamily="Arial"
FontWeight="Bold" FontSize="16" BorderBrush="Black" BorderThickness="0,0,0,3" IsTabStop="False" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblRenewalDate" Content="Renewal Date:" Height="28" Width="119" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="127,60,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold"/>
<TextBox x:Name="tbxRenewalDate" DataContext="{Binding Applicants}" Text="{Binding Path=RenewalDate, Mode=TwoWay}" Height="22" Width="180" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="247,60,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<Label x:Name="lblAddressDisp" Content="Address:" Height="28" Width="80" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="80,115,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold"/>
<TextBox x:Name="tbxAddress1" DataContext="{Binding Applicants, Mode=TwoWay}" Text="{Binding Path=Address1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, BindsDirectlyToSource=True}" Height="22" Width="344" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="162,115,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" TextWrapping="Wrap" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
<TextBox x:Name="tbxAddress2" DataContext="{Binding Applicants}" Text="{Binding Path=Address2, Mode=TwoWay}" Height="22" Width="426" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="80,145,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" TextWrapping="Wrap" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
</grid>
<Grid x:Name="grdButtonsGrid" HorizontalAlignment="Center" Height="80" Width="700" Margin="20, 880,0,0">
<Button x:Name="btnLoadApplicant" Command="{Binding LoadApplicantCommand}"
HorizontalAlignment="Left" Height="43" Margin="60,25,0,0" VerticalAlignment="Top"
Width="78" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
<TextBlock Text="Load Applicant" TextWrapping="Wrap" />
</Button>
<Button x:Name="btnGetApplicant" Command="{Binding GetApplicantCommand}"
HorizontalAlignment="Left" Height="43" Margin="150,25,0,0" VerticalAlignment="Top"
Width="95" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
<TextBlock Text="Get Applicant By Name" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="WrapWithOverflow" />
</Button>
<Button x:Name="btnPrintApplicant" Command="{Binding PrintApplicantCommand}"
HorizontalAlignment="Left" Height="43" Margin="255,25,0,0" VerticalAlignment="Top"
Width="70" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
<TextBlock Text="Print Applicant" TextWrapping="Wrap"/>
</Button>
<Button x:Name="btnClearData" Command="{Binding ClearDataCommand}"
HorizontalAlignment="Left" Height="43" Margin="335,25,0,0" VerticalAlignment="Top"
Width="58" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
<TextBlock Text="Clear Data" TextWrapping="Wrap"/>
</Button>
<Button x:Name="btnClose" Content="Close" Command="{Binding CloseCommand}"
HorizontalAlignment="Left" Height="43" Margin="440,25,0,0" VerticalAlignment="Top"
Width="58" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
</Button>
</Grid>
</grid>
Here is part of my Application class in my ApplicationModel.
namespace ApplicationForm.Model
{
public class ApplicationModel
{
}
public class Applicant : INotifyPropertyChanged
{
private string firstName;
private string lastName;
private string familyO;
private string familyA;
private string familyC;
private string renewalDate;
private string address1;
private string address2;
private string city;
private string state;
private string zip;
private string phoneNumber;
private string name1Birthday;
private string name2;
private string name2Birthday;
private string name3;
private string name3Birthday;
private string name4;
private string name4Birthday;
private string name5;
private string name5Birthday;
private string name6;
private string name6Birthday;
private string name7;
private string name7Birthday;
private string name8;
private string name8Birthday;
public string FirstName
{
get
{
return firstName;
}
set
{
if (firstName != value)
{
firstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
}
public string LastName
{
get { return lastName; }
set
{
if (lastName != value)
{
lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
}
public string FullName
{
get
{
return firstName + " " + lastName;
}
}
public string FamilyO
{
get { return familyO; }
set
{
if (familyO != value)
{
familyO = value;
RaisePropertyChanged("FamilyO");
}
}
}
public string FamilyA
{
get { return familyA; }
set
{
if (familyA != value)
{
familyA = value;
RaisePropertyChanged("FamilyA");
}
}
}
}
}
And here is my print routine in ApplicationViewModel.
public void PrintApplicant(object selectedItem)
{
ApplicationView view = new ApplicationView();
PrintDialog prnt = new PrintDialog();
ObservableCollection<Applicant> applicants = new ObservableCollection<Applicant>();
applicants = Applicants;
if (prnt.ShowDialog() == true)
{
Size pageSize = new Size(prnt.PrintableAreaWidth - 30, prnt.PrintableAreaHeight - 30);
view.grdAppFormGrid.Measure(pageSize);
view.grdAppFormGrid.Arrange(new Rect(5, 5, pageSize.Width, pageSize.Height));
prnt.PrintVisual(view.grdAppFormGrid, applicants[0].FullName);
}
}

Binding in UWP (c#)

I have listview with binding:
<ListView x:Name="OrdersListView" IsItemClickEnabled="True" SelectionMode="Single"
ItemClick="OrdersListView_ItemClick" SelectedItem="{Binding AllRoundsSelectedItem, Mode=TwoWay}">
I need to click element and receive data.
I do it like this.
public RootObject allRoundsSelectedItem;
public RootObject AllRoundsSelectedItem
{
get { return allRoundsSelectedItem; }
set { allRoundsSelectedItem = value; OnPropertyChanged();}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void OrdersListView_ItemClick(object sender, ItemClickEventArgs e)
{
}
When i set breakpoint on this row set { allRoundsSelectedItem = value; OnPropertyChanged();}
I see that I have value that I need to have.
I try to enter data in another ListView.
Here code for it:
<ScrollViewer HorizontalAlignment="Left" Height="667" Margin="415,54,-124,-1" VerticalAlignment="Top" Width="989">
<ListView x:Name="DetailsListView" ItemsSource="{Binding AllRoundsSelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Height="667" Width="989">
<Grid x:Name="DetailGrid" Height="667" Width="989" VerticalAlignment="Top" >
<Grid HorizontalAlignment="Left" Height="44" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Время и дата заказа" VerticalAlignment="Top" Height="44" Width="214" FontSize="23"/>
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding date_created}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,44,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock3" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Статус" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock4" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding status }" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,88,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock5" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Осталось времени" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock6" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,132,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock7" HorizontalAlignment="Left" TextWrapping="Wrap" Text="На когда" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock8" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding billing.city}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,176,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock9" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Сумма" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock10" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding total}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,220,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock11" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Имя" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock12" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding billing.first_name}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,264,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock13" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Телефон" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock14" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding billing.phone}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,308,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock15" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Адрес" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock16" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding billing.address_1}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,352,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock17" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Количество персон" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock18" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="{Binding billing.postcode}" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,396,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock19" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Откуда" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock20" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,440,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock21" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Состав заказа" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock22" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,484,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock23" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Приметка для кухни" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock24" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="44" Margin="0,528,0,0" VerticalAlignment="Top" Width="989" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock x:Name="textBlock25" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Приметка для курьера" VerticalAlignment="Top" Height="44" Width="360" FontSize="23"/>
<TextBlock x:Name="textBlock26" HorizontalAlignment="Left" Margin="500,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="44" Width="360"/>
</Grid>
</Grid>
</StackPanel></DataTemplate></ListView.ItemTemplate></ListView>
But anything shows.
Where is my trouble?
Thank's for help so much!
Your question wasn't very clear, so I can only guess as to what you were trying to achieve. It looks as though you want some kind of master-detail interface, wherein you choose an item from the list (master) and then another form displays with controls for editing the data for that item (detail).
Your first ListView looks fine (and the binding works as you said). But why are you using another ListView for displaying the details of the selected item? ListViews are for displaying lists of data, but it appears that RootObject is not an array or collection class (I could be wrong, I don't know the class definition). You probably shouldn't be using a ListView, but whatever you use, you should bind the AllRoundsSelectedItem to the DataSource property of the panel.
Solution 1: I mostly agree with #Decade Moon, but for the last line, there is no DataSource property of the StackPanel you used, I think this maybe is a typo, it should be DataContext. Anyway, here I'm writing this answer to give a demo for this solution:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView x:Name="OrdersListView" IsItemClickEnabled="True" SelectionMode="Single"
ItemClick="OrdersListView_ItemClick" SelectedItem="{Binding AllRoundsSelectedItem, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ScrollViewer Grid.Column="1">
<StackPanel x:Name="Detail" Grid.Column="1" >
<TextBlock Text="{Binding ItemName}" FontSize="30" Foreground="Red" />
<TextBlock Text="{Binding Paramether1}" Margin="0,15" FontSize="20" />
<TextBlock Text="{Binding Paramether2}" FontSize="20" />
<TextBlock Text="{Binding Paramether3}" Margin="0,15" FontSize="20" />
<TextBlock Text="{Binding Paramether4}" FontSize="20" />
<TextBlock Text="{Binding Paramether5}" Margin="0,15" FontSize="20" />
</StackPanel>
</ScrollViewer>
</Grid>
If your layout is like this, then you can keep your most of the code in behind and in the OrdersListView_ItemClick event code like this:
private void OrdersListView_ItemClick(object sender, ItemClickEventArgs e)
{
AllRoundsSelectedItem = e.ClickedItem as RootObject;
Detail.DataContext = AllRoundsSelectedItem;
}
Solution 2: if you still want to use a ListView to show the details so can they be selected or something else, you can for example code like this:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView x:Name="OrdersListView" IsItemClickEnabled="True" SelectionMode="Single"
ItemClick="OrdersListView_ItemClick" SelectedItem="{Binding AllRoundsSelectedItem, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView x:Name="DetailsListView" ItemsSource="{Binding AllRoundsSelectedItem}" Grid.Column="1">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}" FontSize="20" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
And the type of AllRoundsSelectedItem should be List or OberservableCollection, it means you need to modify your code behind for example like this:
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public MainPage ()
{
this.InitializeComponent();
this.DataContext = this;
}
private ObservableCollection<RootObject> collection = new ObservableCollection<RootObject>();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
for (int i = 0; i < 100; i++)
{
collection.Add(new RootObject
{
ItemName = "Item " + i,
Paramether1 = "Paramether1 " + i,
Paramether2 = "Paramether2 " + i,
Paramether3 = "Paramether3 " + i,
Paramether4 = "Paramether4 " + i,
Paramether5 = "Paramether5 " + i,
});
}
OrdersListView.ItemsSource = collection;
}
public ObservableCollection<string> allRoundsSelectedItem;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> AllRoundsSelectedItem
{
get { return allRoundsSelectedItem; }
set
{
allRoundsSelectedItem = value;
OnPropertyChanged();
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void OrdersListView_ItemClick(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as RootObject;
AllRoundsSelectedItem = new ObservableCollection<string>
{
item.ItemName,item.Paramether1,
item.Paramether2, item.Paramether3,
item.Paramether4, item.Paramether5,
};
}
}
By my side the RootObject class is like this:
public class RootObject
{
public string ItemName { get; set; }
public string Paramether1 { get; set; }
public string Paramether2 { get; set; }
public string Paramether3 { get; set; }
public string Paramether4 { get; set; }
public string Paramether5 { get; set; }
}
There are maybe other solutions, but you will need either modify your xaml code or the data type of your AllRoundsSelectedItem.

Making something like Fragment in UWP

I writing app for UWP.
I have app very similar to Windows 10 Mail app.
I have this UI
Here is xaml of this screen
<Grid BorderBrush="White" BorderThickness="1">
<Grid.Background>
<ImageBrush Stretch="Fill" ImageSource="Images/Background.png"/>
</Grid.Background>
<Grid HorizontalAlignment="Left" Height="720" VerticalAlignment="Top" Width="60" BorderBrush="#FFF5F1F1" BorderThickness="0,0,1,0">
<Button x:Name="MenuButton" Content="" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="38" Width="38">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/Menu-100.png"/>
</Button.Background>
</Button>
<Button x:Name="logoutbutton" Content="" HorizontalAlignment="Left" Margin="10,650,0,0" VerticalAlignment="Top" Height="43" Width="38">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="Images/Logout_Button.png"/>
</Button.Background>
</Button>
</Grid>
<Grid HorizontalAlignment="Left" Height="47" Margin="63,2,-121,0" VerticalAlignment="Top" Width="1338" BorderBrush="#FFFDFDFD" Padding="0,0,0,1" BorderThickness="0,0,0,1">
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="В Работе" VerticalAlignment="Top" Height="47" Width="1218" FontSize="32" FontFamily="SF UI Display" Padding="550,0,0,0" Foreground="White"/>
</Grid>
<ScrollViewer HorizontalAlignment="Left" Height="668" Margin="63,52,0,0" VerticalAlignment="Top" Width="350">
<GridView x:Name="OrdersGridView" >
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel >
<Grid Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,1,1">
<TextBlock Text="{Binding date_created}" HorizontalAlignment="Left" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Width="350" Height="50" FontFamily="SF UI Display" FontSize="25" FontWeight="Light" Foreground="White" />
<TextBlock TextAlignment="Center" HorizontalAlignment="Left" Margin="0,146,-1,0" TextWrapping="Wrap" Text="{Binding billing.address_1}" VerticalAlignment="Top" Height="58" Width="350" FontFamily="SF UI Display" FontSize="25" FontWeight="Light" Foreground="White" />
<TextBlock HorizontalAlignment="Left" TextAlignment="Center" Margin="0,86,-1,0" TextWrapping="Wrap" Text="{Binding billing.first_name}" VerticalAlignment="Top" Height="60" Width="350" FontFamily="SF UI Display" FontSize="25" FontWeight="Light" Foreground="White" Padding="0,0,0,0"/>
</Grid>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</ScrollViewer>
</Grid>
I make Binding in code like this:
public async void Down()
{
RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754");
WCObject wc = new WCObject(rest);
//Get all products
var orders = await wc.GetOrders(new Dictionary<string, string>() {
{ "per_page", "100" }, { "status","processing"} }); // Dodelat filtr dlaya teh chto v rabote
string products = orders.ToFormattedJsonString();
List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);
foreach (RootObject root in rootObjectData)
{
string date = root.date_created;
string name = root.billing.first_name;
Orders = new ObservableCollection<RootObject>(rootObjectData) { new RootObject { date_created = date}
};
OrdersGridView.ItemsSource = Orders;
};
}
}
}
I need to open smth like Fragment.
Here is UI how it need to be like:
How I can do this?
Thank's for help so much!

How to display image to my listbox from my database

The property in the database is image. What's in the content is binary data. This is the datatemplate of the listbox:
<DataTemplate x:Key="ItemTemplate">
<Grid Width="467" Height="123" Margin="5,0,0,0">
<Image Name="imgProduct" Source="{Binding pImage}" HorizontalAlignment="Left" Margin="0,5" Width="115"/>
<TextBlock Text="{Binding pName}" Margin="120,0,133,92" Foreground="Black" FontFamily="Tahoma" FontSize="22.667"/>
<TextBlock Text="{Binding pPrice}" Margin="146,69,263,35" Foreground="Black" FontSize="13.333"/>
<TextBlock Text="{Binding pQty}" Margin="232,30,183,76" Foreground="#FF11BB00" FontSize="13.333" FontFamily="Tahoma"/>
<TextBlock Text="{Binding pRewardCost}" Margin="433,89,0,14" FontSize="13.333" Foreground="#7F000000"/>
<TextBlock Text="{Binding pSalesPrice}" Margin="155,85,233,14" Foreground="#FF005910" FontSize="17.333" RenderTransformOrigin="-0.196,0.375"/>
<TextBlock HorizontalAlignment="Left" Margin="120,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="13.333" Height="15" Width="114">
<Run Text="Quantity Available:"/>
<LineBreak/>
<Run Text=":"/>
</TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="120,69,0,0" TextWrapping="Wrap" Text="SGD" VerticalAlignment="Top" FontSize="13.333" RenderTransformOrigin="-1.923,0.625" Foreground="Black"/>
<TextBlock HorizontalAlignment="Left" Margin="120,85,0,0" TextWrapping="Wrap" Text="SGD" VerticalAlignment="Top" FontSize="17.333" RenderTransformOrigin="-1.923,0.625"/>
<TextBlock HorizontalAlignment="Left" Margin="318,89,0,0" TextWrapping="Wrap" Text="Reward Points cost:" VerticalAlignment="Top" FontSize="13.333" Foreground="#7F000000"/>
<InkPresenter HorizontalAlignment="Left" Height="2" Margin="122,75,0,0" VerticalAlignment="Top" Width="74" Background="Black" RenderTransformOrigin="0.5,0.5">
<InkPresenter.RenderTransform>
<CompositeTransform ScaleY="-1"/>
</InkPresenter.RenderTransform>
</InkPresenter>
<InkPresenter HorizontalAlignment="Left" Height="2" Margin="0,117,0,0" VerticalAlignment="Top" Width="467" Background="#66000000"/>
</Grid>
</DataTemplate>
The C# Code that consumes the database and shows as items on the listbox is as below:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var pdtQry = from pdt in fypDB.Products
select pdt;
productCollection = new DataServiceCollection<Product>();
productCollection.LoadCompleted += new
EventHandler<LoadCompletedEventArgs>(productCollection_LoadCompleted);
productCollection.LoadAsync(pdtQry);
}
void productCollection_LoadCompleted(Object sender, LoadCompletedEventArgs e)
{
if (productCollection.Continuation != null) productCollection.LoadNextPartialSetAsync();
else
{
listboxSearch.ItemsSource = productCollection;
}
}
After debugging, pName, pPrice, pQty, pRewardCost, and pSalesPrice. However the image is not displayed. I think it may be because the image is not of the same format. The format in dbo is image and is <binary data>. Is there any way to convert and display it? Thanks in advance!!

Categories