WPF Sparrow Chart - c#

Hello i've got a problem with generating a simple chart with Sparrow Toolkit in WPF. It's my XAML
<Window x:Class="GeneratorWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sparrow="http://sparrowtoolkit.codeplex.com/wpf"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Generator" Height="500" Width="1000" Loaded="button1_Click">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="79,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="Wariancja" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Średnia" Height="28" HorizontalAlignment="Left" Margin="12,46,0,0" Name="label2" VerticalAlignment="Top" />
<Label Content="Przedział" Height="28" HorizontalAlignment="Left" Margin="12,80,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="79,46,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="79,85,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<Button Content="Generuj" Height="23" HorizontalAlignment="Left" Margin="380,13,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<sparrow:SparrowChart Theme="Grayscale" OverlayMode="SeriesFirst" Width="800" Height="300" VerticalAlignment="Bottom" Name="chart">
<sparrow:SparrowChart.Legend>
<sparrow:Legend Header="Legend" LegendPosition="Outside" HorizontalAlignment="Right" VerticalAlignment="Top" ShowIcon="True"></sparrow:Legend>
</sparrow:SparrowChart.Legend>
<sparrow:SparrowChart.XAxis>
<sparrow:LinearXAxis MinValue="0" MaxValue="10" MajorTicksPosition="Cross"></sparrow:LinearXAxis>
</sparrow:SparrowChart.XAxis>
<sparrow:SparrowChart.YAxis>
<sparrow:LinearYAxis MinValue="0" MaxValue="10" MajorTicksPosition="Cross"></sparrow:LinearYAxis>
</sparrow:SparrowChart.YAxis>
</sparrow:SparrowChart>
</Grid>
And .cs
private void button1_Click(object sender, RoutedEventArgs e)
{
//float wariancja = 0;
//float srednia = 0;
//int przedzial = 0;
textBox1.Text = "asdasd";
chart.Series["as"];
var point = new Sparrow.Chart.ChartPoint();
var asss = new Sparrow.Chart.AreaSeries()
{
Points = new Point()
{
X = 1,
Y = 4
},
};
var serie = new SeriesBase();
chart.Series.Add();
}
I'm trying to create object but nothings work. Can someone help me? Can i use only cs to generate chart? I need to use bind ? I'm start with WPF and don't really know about it too much

You assign point, asss and serie but you never use them.
chart.Series["as"]; has no effect.
chart.Series.Add(); looks wrong, should that be chart.Series.Add(point);?

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())
{
...
}
}

How to access to elements in WPF by code?

I try to access to inputs value in wpf project but get me error that :
The name 'txt_SlaveNumber' does not exist in the current context
namespace SPMS_WIN
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn_RunModbus_Click(object sender, RoutedEventArgs e)
{
byte SlaveNumber = byte.Parse(txt_SlaveNumber.Text);
byte FunctioCode = byte.Parse(txt_FunctionCode.Text);
int StartIndex = Int32.Parse(txt_StartIndex.Text);
int DataLength = Int32.Parse(txt_DataLength.Text);
Modbus modbus = new Modbus();
modbus.ModBusCommand(SlaveNumber,FunctioCode,StartIndex,DataLength);
}
}
}
<Window x:Class="SPMS_WIN.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:SPMS_WIN"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="30,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_SlaveNumber"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="196,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_FunctionCode"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="361,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_StartIndex"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="543,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_DataLength"/>
<Label Content="Slave Address
" HorizontalAlignment="Left" Margin="30,72,0,0" VerticalAlignment="Top" Width="120" Height="26"/>
<Label Content="Function Code" HorizontalAlignment="Left" Margin="196,72,0,0" VerticalAlignment="Top" Width="120"/>
<Label Content="Start Index" HorizontalAlignment="Left" Margin="361,72,0,0" VerticalAlignment="Top" Width="120"/>
<Label Content="Data Length" HorizontalAlignment="Left" Margin="543,72,0,0" VerticalAlignment="Top" Width="120"/>
<Button Content="Run" HorizontalAlignment="Left" Margin="30,142,0,0" VerticalAlignment="Top" Width="75" AutomationProperties.Name="btn_RunModbus" Click="btn_RunModbus_Click"/>
</Grid>
</Window>
What is problem?
Maybe if you change :
<TextBox HorizontalAlignment="Left" Height="23" Margin="30,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" AutomationProperties.Name="txt_SlaveNumber"/>
to :
<TextBox HorizontalAlignment="Left" Height="23" Margin="30,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Name="txt_SlaveNumber"/>
It would resolve your problem.
The problem is the tag generated for the name
AutomationProperties.Name
Replace it with
x:Name
and it should be ok.

WPF throws System.NullReferenceException when trying to modify xaml elements

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.

WPF Move grid Error

I'm trying something new. I've got a few grids, and when I try to grab a grid i want to move its parent around my top grid. At the moment I get a System.NullReferenceException on m_startOffset = new Vector(translate.X, translate.Y); in the Grid_MouseDown method. Does someone know how i should tackle this problem?
UI:
<Grid x:Name="GridHost">
<Grid x:Name="GRLogin" Margin="1401,292,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="501" d:IsHidden="True" Focusable="True">
<Grid Height="30" VerticalAlignment="Top" Background="#FF1585B5" Margin="0" MouseLeftButtonUp="Grid_MouseUp" MouseLeftButtonDown="Grid_MouseDown" MouseMove="Grid_MouseMove" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="GRLoginClose" Content="X" Background="{x:Null}" Foreground="White" FontWeight="Bold" OpacityMask="Black" Margin="470,0,0,0" Width="30" Height="30" Click="Close"/>
<Button Content="-" Background="{x:Null}" Foreground="White" FontWeight="Bold" OpacityMask="Black" Margin="0,0,30,0" Width="30" Height="30" HorizontalAlignment="Right"/>
<Label Content="Login" HorizontalAlignment="Center" Margin="0" FontWeight="Bold"/>
</Grid>
<Grid HorizontalAlignment="Left" Height="250" Margin="0,30,0,0" VerticalAlignment="Top" Width="500" Background="#FF262626">
<PasswordBox HorizontalAlignment="Center" Margin="206,130,120,94" VerticalAlignment="Center" Width="174"/>
<Label Content="Password" HorizontalAlignment="Center" Margin="122,130,308,94" VerticalAlignment="Center" RenderTransformOrigin="-1.737,0.462" Width="70"/>
<Label Content="Username" HorizontalAlignment="Center" Margin="122,99,308,125" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Width="70"/>
<TextBox HorizontalAlignment="Center" Height="23" Margin="206,99,120,125" TextWrapping="Wrap" VerticalAlignment="Center" Width="174" RenderTransformOrigin="0.467,-0.346"/>
<Button Content="Login" HorizontalAlignment="Left" VerticalAlignment="Top" Width="258" Margin="122,180,0,0" Height="40" Background="#FF1585B5" Foreground="White" FontWeight="Bold"/>
</Grid>
</Grid>
Movement code:
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = ((Grid)sender).Parent as Grid;
TranslateTransform translate = element.RenderTransform as TranslateTransform;
m_start = e.GetPosition(GridHost);
m_startOffset = new Vector(translate.X, translate.Y);
element.CaptureMouse();
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = ((Grid)sender).Parent as Grid;
TranslateTransform translate = element.RenderTransform as TranslateTransform;
if (element.IsMouseCaptured)
{
Vector offset = Point.Subtract(e.GetPosition(GridHost), m_start);
translate.X = m_startOffset.X + offset.X;
translate.Y = m_startOffset.Y + offset.Y;
}
}
private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = ((Grid)sender).Parent as Grid;
element.ReleaseMouseCapture();
}
Declare the parent Grid's RenderTransform in the XAML:
<Grid x:Name="GRLogin" Margin="1401,292,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="501" d:IsHidden="True" Focusable="True">
<Grid.RenderTransform>
<TranslateTransform/>
</Grid.RenderTransform>
...
</Grid>
You could also check the RenderTransform for null, in which case you set it: element.RenderTransform = new TranslateTransform();

Datagrid not showing data in wpf with c#

I am trying to use a dataGrid in WPF with c#. But I cannot get my datagrid to show any of my data in the table when I run my program in debug mode. I have this code executing when the datagrid loads. But all I see is an empty square.
private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
var items = new List<SaveTable>();
items.Add(new SaveTable("A" , 0));
items.Add(new SaveTable("B" , 0));
items.Add(new SaveTable("C" , 0));
items.Add(new SaveTable("D" , 0));
items.Add(new SaveTable("E" , 0));
var grid = sender as DataGrid;
grid.ItemsSource = items;
}
I save a class named SaveTable which looks like this:
class SaveTable
{
public string Name { get; set; }
public double Value { get; set; }
public SaveTable(string name, double value)
{
this.Name = name;
this.Value = value;
}
}
I got this code format online and it seems like everything is right? any suggestions?
here is the xaml code for that window
<Window x:Class="RobustCalculator.Storage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Storage" Height="499" Width="546" Activated="Window_Activated" Loaded="Window_Loaded">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="A" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="24,10,0,306" Name="valueA" Width="120" TextChanged="valueA_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,39,0,0" Name="valueB" VerticalAlignment="Top" Width="120" TextChanged="valueB_TextChanged" />
<TextBlock Height="20" HorizontalAlignment="Left" Margin="10,42,0,0" Name="textBlock2" Text="B" VerticalAlignment="Top" Width="20" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,71,0,0" Name="valueC" VerticalAlignment="Top" Width="120" TextChanged="valueC_TextChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,72,0,0" Name="textBlock3" Text="C" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,101,0,0" Name="textBlock4" Text="D" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,98,0,0" Name="valueD" VerticalAlignment="Top" Width="120" TextChanged="valueD_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,130,0,0" Name="valueE" VerticalAlignment="Top" Width="120" TextChanged="valueE_TextChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="11,131,0,0" Name="textBlock5" Text="E" VerticalAlignment="Top" />
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="235,130,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" SelectionChanged="dataGrid1_SelectionChanged" Loaded="dataGrid1_Loaded" />
</Grid>
I added a breakpoint and the code isn't being executed. I am using the Loaded event, should I be using a different event?
Your code works fine with Loaded event but at the moment you set AutoGenerateColumns="False" and don't define columns. You need to either define columns manually
<DataGrid AutoGenerateColumns="False" ... Loaded="dataGrid1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Value}" Header="Value"/>
</DataGrid.Columns>
</DataGrid>
or let it auto generate columns
<DataGrid AutoGenerateColumns="True" ... Loaded="dataGrid1_Loaded"/>

Categories