I've spent some time coding in Java, and I'd like to make the switch to C#. I decided to try and port over a simple math flash cards program I wrote in Java and everything is going okay but the XAML portion. I'm a little confused about data binding and the proper use of triggers. Here's what I have:
XAML:
[code]
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Flash_Cards" xmlns:Properties="clr-namespace:Flash_Cards.Properties" x:Class="Flash_Cards.MainWindow"
Title="Flash Cards" Height="650" Width="450" ResizeMode="NoResize">
<Grid x:Name="MainWin">
<ComboBox Name="ProbTypes" HorizontalAlignment="Center" VerticalAlignment="Top" Width="300" IsReadOnly="True" Margin="0,80,0,0" FontSize="14"
SelectedIndex="2" SelectionChanged="TypeSelected">
<ComboBoxItem Content="Addition"/>
<ComboBoxItem Content="Subtraction"/>
<ComboBoxItem Content="Addition and Subtraction"/>
<ComboBoxItem Content="Multiplication"/>
<ComboBoxItem Content="Division"/>
<ComboBoxItem Content="Multiplication and Division"/>
</ComboBox>
<StackPanel x:Name="OptionPanelA" HorizontalAlignment="Left" Height="142" Margin="116,332,0,0" VerticalAlignment="Top" Width="210" Background="Black">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=optPanel}" Value="1">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
</Grid>
</Window>
[/code]
The code behind:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Flash_Cards
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string optPanel = "0";
public MainWindow()
{
InitializeComponent();
}
public void TypeSelected(object sender, SelectionChangedEventArgs e)
{
FlashCards.typeSelected = ProbTypes.SelectedIndex;
if (FlashCards.typeSelected < 3)
{
optPanel = "0";
}
else
{
optPanel = "1";
}
Console.Out.WriteLine(optPanel); //I used this to troubleshoot.
}
//The console will print the correct value
}
//when the index changes but the panel will
}
//does not become hidden
[/code]
and the flash card code:
[code]
using System;
namespace Flash_Cards
{
public class FlashCards
{
public static int typeSelected = 2;
public static int numCap;
public static int nFirst;
public static int nSecond;
public static int nAnswer;
public static int totalProbs;
public void NextCard(int typeSelected, int numCap)
{
int x;
int y;
int z;
int nSwitch;
int randProb;
Random randNum = new Random(); //generates 2 random numbers and
if (typeSelected < 3)
{
//determines whether to add or multiply
x = randNum.Next(numCap + 1); //based on the value of 'typeSelected'
y = randNum.Next(numCap + 1);
z = x + y;
}
else
{
x = randNum.Next(numCap + 1);
y = randNum.Next(numCap + 1);
z = x * y;
}
switch (typeSelected)
{
//generates new flash card, math function depends on value of typeSelected
case 0:
if (x > y)
{
//put the larger number on top when adding
nSwitch = x;
x = y; //ADDITION<--- Mathematical functions denoted by CAPS
y = nSwitch;
}
nFirst = x;
nSecond = y;
nAnswer = z;
break;
case 1:
nFirst = z;
nSecond = x; //SUBTRACTION
nAnswer = y;
break;
case 2:
randProb = randNum.Next(2); //for combined problem types, assign a random value
if (randProb == 0)
{
//of 0 or 1 to randProb...
if (x > y)
{
nSwitch = x;
x = y;
y = nSwitch;
}
nFirst = x; //ADDITION AND SUBTRACTION
nSecond = y;
nAnswer = z;
}
//the randProb value is used here to create
else
{
//either addition or subtraction problem
nFirst = z;
nSecond = x;
nAnswer = y;
}
break;
case 3:
if (x > y)
{
//put larger number on top when multiplying
nSwitch = x; //MULT
x = y;
y = nSwitch;
}
nFirst = x;
nSecond = y;
nAnswer = z;
break;
case 4:
nFirst = z; //DIV
nSecond = x;
nAnswer = y;
break;
case 5:
randProb = randNum.Next(2);
if (randProb == 0)
{
//MULT AND DIV
if (x > y)
{
nSwitch = x;
x = y;
y = nSwitch;
}
nFirst = x;
nSecond = y;
nAnswer = z;
}
else
{
nFirst = z;
nSecond = x;
nAnswer = y;
}
break;
}
ShowCard(nFirst, nSecond, nAnswer); //after generating a new card, pass it to
ShowCard()
}
public void ShowCard(int nFirst, int nSecond, int nAnswer) //organize and present the new card
{
}
}
}
[/code]
My problem is that I can't get the StackPanel (potentially the Option Panel) to change when the value of optPanel changes... Thanks in advance for any advice or help.