MainWindow.xaml
<CheckBox Content="Enable" Height="16" HorizontalAlignment="Left" Margin="190,40,0,0" Name="checkBox_Enable" VerticalAlignment="Top" IsChecked="True" Unchecked="checkBox_Enable_Unchecked" Checked="checkBox_Enable_Checked" />
<Label Content="Fullscreen:" Height="15" HorizontalAlignment="Left" Margin="227,63,0,0" Name="label3" VerticalAlignment="Top" Width="56" Padding="1" />
<TextBox HorizontalAlignment="Right" Margin="0,86,243,0" Name="textBox_Hotkey_Fullscreen" Width="33" Height="18" VerticalAlignment="Top" />
<Label Content="Custom field:" HorizontalAlignment="Left" Margin="227,110,0,136" Name="label5" Padding="1" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="227,131,0,0" Name="textBox_Hotkey_Customfield" VerticalAlignment="Top" Width="33" />
<Label Content="Window-related:" HorizontalAlignment="Left" Margin="227,155,0,87" Name="label4" Padding="1" />
<TextBox HorizontalAlignment="Left" Margin="227,0,0,112" Name="textBox_Hotkey_Windowrelated" Width="33" Height="18" VerticalAlignment="Bottom" />
MainWindow.xaml.cs
private void checkBox_Enable_Unchecked(object sender, RoutedEventArgs e)
{
textBox_Hotkey_Fullscreen.IsEnabled = false;
textBox_Hotkey_Customfield.IsEnabled = false;
textBox_Hotkey_Windowrelated.IsEnabled = false;
}
//There are no problems after purging this function. However, the controls couldn't be "re-enabled", what obviously makes my concept pointless.
private void checkBox_Enable_Checked(object sender, RoutedEventArgs e)
{
textBox_Hotkey_Fullscreen.IsEnabled = true;
textBox_Hotkey_Customfield.IsEnabled = true;
textBox_Hotkey_Windowrelated.IsEnabled = true;
}
Why does it throw the NullReferenceException just after launch?
I would suggest use Binding like (remove event handlers from xaml)
IsEnabled="{Binding ElementName=checkBox_Enable, Path=IsChecked}"
for every control you want to enable/disable with the checkbox.
Why does it throw the NullReferenceException just after launch?
Because not all controls are loaded yet when the CheckBox fires the event.
I would suggest binding the CheckBox to a bool and binding the other controls' IsEnabled to that (or bind directly to the CheckBox).
Related
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())
{
...
}
}
enter code hereHi I am using a Flyout in my UWP App. On a button click I am showing list of items in flyout. when I click the button the flyout is getting opened.
But here I want to open flyout when the list is not empty. If the list is empty I want to hide the flyout.
For this I have written the code but hiding is not working.
Can Any one has idea about this.
xaml code:
<Button TabIndex="4" Background="#212121" Name="btnCashPay" Click="btnCashPay_Tapped" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="#212121" BorderThickness="0" Margin="0,-5,0,0" >
<Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="/Images/pay_bill(30_30).png" Stretch="None"/>
<Button.Flyout>
<Flyout x:Name="flyout" FlyoutPresenterStyle="{StaticResource Flyoutstyle}">
<StackPanel>
<TextBlock Grid.Row="0" Height="35" HorizontalAlignment="Center" Foreground="DarkTurquoise" FontWeight="SemiBold">Please Add Free Items To Cart </TextBlock>
<Border x:Name="dgFreeItemsug" BorderThickness="0.5" Visibility="Visible" BorderBrush="LightSlateGray" Grid.Row="1" Background="White" Height="200" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="10,-16,5,0">
<ScrollViewer x:Name="svFreeItemSugg" HorizontalScrollBarVisibility="Hidden" Padding="0" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" Margin="0,0,0,0">
<controls:DataGrid x:Name="dgFreeItem" Height="200" HorizontalAlignment="Stretch"
controls:DataGridExtensions.UseSingleSelectionAndDeselection="true" VerticalAlignment="Top" RowBackgroundEvenBrush="White" RowBackgroundOddBrush="White" Margin="0,0,0,0" Navigate="dgFreeItem_Navigate">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn x:Name="freeitemddesc" Width="1*" Binding="{Binding DealSku}">
</controls:DataGridTextColumn>
<controls:DataGridTextColumn x:Name="freeitemprice" Width="2*" Binding="{Binding DealDescription}">
</controls:DataGridTextColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
</ScrollViewer>
</Border>
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
xaml.cs code:
private void btnCardPay_Tapped(object sender, RoutedEventArgs e)
{
txtcardmessage.Text = string.Empty;
media.Play();
if (objfreeitemlist == null)
btnCardPay.Flyout.Hide();
}
You can define your Flyout as a Button Resource
<Button x:Name="MyButton" Content="Button" Tapped="Button_Tapped" >
<Button.Resources>
<Flyout x:Name="MyFlyout">
....
</Flyout>
</Button.Resources>
</Button>
This way you have to open the Flyout yourself, but you can define when to open it.
private void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
var button = sender as Button;
if (button != null && objfreeitemlist != null)
{
MyFlyout.ShowAt(button);
}
}
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);?
have a bit of a problem understanding some usercontrol interaction:
I've built this color picker. I have four sliders that change the color in Red, Green, & Blue (RGB and all that) as well as its opacity(alpha). When I slide these back and forth, I get a live response from the previewColor Rectangle(the fill color of the rectangle changes as I slide any of the sliders). All these elements are inside my user control.
In my main window, I have two signigicant elements, a blank canvas and a "change canvas" button. Ideally, when I play with the sliders in my user control and find a color i like, I would just click the ChangeCanvas button and the canvas background would change to match the current color of the previewRectangle.
The problem is that I cannot figure out how to bind the current color of the previewRectangle to a button action that my canvas will accept. Is there any way to bind the background of my canvas to the fill of my rectangle via button action? Or is it better to pass the color property from the usercontrol to the mainwindow?
Main Window XAML & Code
<Window x:Class="C_ShapeCanvasV2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:schafec="clr-namespace:C_ShapeCanvasV2.SControl"
Title="ColorCanvas" Height="393" Width="729">
<Grid Background="Gray">
<Canvas Name="MainCanvas" MouseLeftButtonDown="MainCanvas_MouseLeftButtonDown" ClipToBounds="True" MouseRightButtonDown="MainCanvas_MouseRightButtonDown" Background="White" HorizontalAlignment="Left" Height="363" VerticalAlignment="Top" Width="305"/>
<!-- user control -->
<schafec:ColorControls x:Name="colorControls" Margin="330,21,23,189"/>
<!-- user control -->
<Button Name="ChangeCanvas" Click="ChangeCanvas_Click" Content="Change Canvas" HorizontalAlignment="Left" Margin="456,209,0,0" VerticalAlignment="Top" Width="158" />
<Button Name="clearButton" Click="clearButton_Click" Content="Clear" HorizontalAlignment="Left" Margin="498,236,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
namespace C_ShapeCanvasV2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//---------------------------------------------------------------------------------------------//
private void ChangeCanvas_Click(object sender, RoutedEventArgs e)
{
//attempted -- not working
//MainCanvas.Background = new SolidColorBrush(clr);
}
private void clearButton_Click(object sender, RoutedEventArgs e)
{
MainCanvas.Children.Clear();
MainCanvas.Background = new SolidColorBrush(Colors.White);
}
}
}
UserControl XAML & Code
<UserControl x:Class="C_ShapeCanvasV2.SControl.ColorControls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="153" Width="368">
<Grid Name="mainGrid" Background ="Gray" Margin="0,6,0,-6">
<!-- just an image under the previewRectangle to provide contrast for opacity purposes -->
<Rectangle Name="underImage" HorizontalAlignment="Left" Height="58" Margin="24,52,0,0" Stroke="Black" VerticalAlignment="Top" Width="64">
<Rectangle.Fill>
<ImageBrush ImageSource ="/C_ShapeCanvasV2;component/Images/ops.png"/>
</Rectangle.Fill>
</Rectangle>
<!-- PreviewColor Rectangle -->
<Rectangle Name="previewColor" HorizontalAlignment="Left" Height="58" Margin="24,52,0,0" Stroke="Black" VerticalAlignment="Top" Width="64"/>
<Slider Name="redSlider" Minimum="0" ValueChanged="redSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,36,0,0" VerticalAlignment="Top" Width="104"/>
<Slider Name="greenSlider" Minimum="0" ValueChanged="greenSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,61,0,0" VerticalAlignment="Top" Width="104" />
<Slider Name="blueSlider" Minimum="0" ValueChanged="blueSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,86,0,0" VerticalAlignment="Top" Width="104"/>
<Slider Name="alphaSlider" Minimum="0" ValueChanged="alphaSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,111,0,0" VerticalAlignment="Top" Width="104"/>
<Label Content="Red" HorizontalAlignment="Left" Margin="112,34,0,0" VerticalAlignment="Top"/>
<Label Content="Green" HorizontalAlignment="Left" Margin="112,57,0,0" VerticalAlignment="Top"/>
<Label Content="Blue" HorizontalAlignment="Left" Margin="112,82,0,0" VerticalAlignment="Top"/>
<Label Content="Alpha" HorizontalAlignment="Left" Margin="112,107,0,0" VerticalAlignment="Top"/>
<Label Name="redLabel" Content="{Binding ElementName= redSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,32,0,0" VerticalAlignment="Top"/>
<Label Name="greenLabel" Content="{Binding ElementName= greenSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,59,0,0" VerticalAlignment="Top"/>
<Label Name="blueLabel" Content="{Binding ElementName= blueSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,82,0,0" VerticalAlignment="Top"/>
<Label Name="alphaLabel" Content="{Binding ElementName= alphaSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,107,0,0" VerticalAlignment="Top"/>
</Grid>
namespace C_ShapeCanvasV2.SControl
{
public partial class ColorControls : UserControl
{
public Color clr;
public ColorControls()
{
InitializeComponent();
}
private void changeColorSystem()
{
clr = Color.FromArgb(Convert.ToByte(alphaSlider.Value), Convert.ToByte(redSlider.Value), Convert.ToByte(greenSlider.Value), Convert.ToByte(blueSlider.Value));
previewColor.Fill = new SolidColorBrush(clr);
}
//public void setColor(Color g)
//{
// Color c = g;
//}
//public Color getColor()
//{
// return clr;
//}
private void alphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
private void blueSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
private void greenSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
private void redSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
}
}
That would not be clean in my opinion, but this piece of code should work:
private void ChangeCanvas_Click(object sender, RoutedEventArgs e)
{
MainCanvas.Background = colorControls.previewColor.Fill;
}
Still you should really inform yourself about MVVM architecture to do it in a more clean/testable way.
Hope it helps.
I have the following DataTemplate:
<DataTemplate x:Key="ToDoListBoxItemTemplate">
<Grid x:Name="item2Expanded" HorizontalAlignment="Left" VerticalAlignment="Top" Width="480" Background="{Binding Converter={StaticResource RowColour}}" MinHeight="81">
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Width="420" Margin="60,0,0,0">
<TextBox x:Name="taskTitle" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ItemName}" VerticalAlignment="Top" Width="420" Background="{x:Null}" BorderBrush="{x:Null}" CaretBrush="#FF0080FF" SelectionBackground="#FFCFCFCF" Foreground="#FF4E4E4E" BorderThickness="3,3,3,6" FontSize="29.333" Style="{StaticResource listTextBoxTemplate}" InputScope="Text" SelectionForeground="#FF4E4E4E" KeyUp="taskTitle_KeyUp" LostFocus="taskTitle_LostFocus" Tap="taskTitle_Tap" IsReadOnly="True" Margin="0,1,0,0" DoubleTap="taskTitle_DoubleTap"/>
<TextBox x:Name="taskDetail" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Tea is an essential English beverage, it has a nice calming effect, and is often served alongside biscuits." VerticalAlignment="Top" Width="420" Background="{x:Null}" BorderBrush="{x:Null}" CaretBrush="#FF0080FF" SelectionBackground="#FFCFCFCF" Foreground="#FF878787" BorderThickness="3,0,3,6" FontSize="21.333" Style="{StaticResource listTextBoxTemplate}" InputScope="Text" SelectionForeground="#FF878787" Margin="0,-20,0,0" KeyUp="taskDetail_KeyUp" LostFocus="taskDetail_LostFocus" Padding="2,5,2,2" IsHitTestVisible="False"/>
<Grid Height="170" Margin="0,-20,0,0">
<Button x:Name="chooseDateButton" Content="27/06/2013" HorizontalAlignment="Left" Margin="6,13,0,0" VerticalAlignment="Top" BorderBrush="#FF959595" Foreground="#FF959595" Width="157" HorizontalContentAlignment="Left" FontSize="20" Style="{StaticResource selectorButtonTemplate}"/>
<Button x:Name="chooseTimeButton" Content="12:00" HorizontalAlignment="Left" Margin="146,13,0,0" VerticalAlignment="Top" BorderBrush="#FF959595" Foreground="#FF959595" Width="99" HorizontalContentAlignment="Left" FontSize="20" Style="{StaticResource selectorButtonTemplate}"/>
<Button x:Name="setOrClearButton" Content="REMIND ME" HorizontalAlignment="Left" Margin="228,13,0,0" VerticalAlignment="Top" BorderBrush="#FF959595" Foreground="White" Width="180" FontSize="20" Background="#FF959595" Style="{StaticResource greyButtonTemplate}"/>
<Button x:Name="deleteButton" Content="DELETE TASK" HorizontalAlignment="Left" Margin="6,85,0,0" VerticalAlignment="Top" BorderBrush="#FFEE4747" Foreground="White" Width="180" FontSize="20" Background="#FFEE4747" Style="{StaticResource redButtonTemplate}"/>
<Image x:Name="retractButton" Margin="347,107,21,11" Source="/Assets/retract.png" Stretch="Fill" Tap="retractButton_Tap"/>
</Grid>
</StackPanel>
<CheckBox x:Name="checkBox" IsChecked="{Binding IsComplete, Mode=TwoWay}" Content="" HorizontalAlignment="Left" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Width="72" BorderThickness="0" Template="{StaticResource checkBoxTemplate}" Checked="checkBox_Checked" Unchecked="checkBox_Unchecked"/>
</Grid>
</DataTemplate>
Where the Grid item2Expanded is placed dynamically in a ListBox (Name="allToDoItemsListBox"). Text is added to each item via bindings.
The image retractButton has Tap="retractButton_Tap", As shown in the code:
private void retractButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (isItemExpanded == true)
{
// Compacts current item
itemGrid.Height = taskTitle.ActualHeight; // Restores itemGrid height to fit only taskTitle
taskTitle.IsReadOnly = true; // taskTitle becomes only double-tap editable, single tap to expand once more
taskDetail.IsHitTestVisible = false; // Stops overlapping taps
isItemExpanded = false;
}
// Adds the event handler for single tap event
tapTimer.Tick += new EventHandler(tapTimer_Tick);
tapTimer.Start();
}
private void tapTimer_Tick(object sender, EventArgs e)
{
// Stop timer
tapTimer.Tick -= new EventHandler(tapTimer_Tick);
tapTimer.Stop();
// Rest of the single tap function
if (isItemExpanded == false)
{
taskDetail.IsHitTestVisible = true;
taskDetail.IsEnabled = false;
// Expands current item
itemGrid.Height = double.NaN; // Sets itemGrid height to auto
isItemExpanded = true;
// Yeah... don't ask.
// Stops temporary text highlighting/auto jumping to keyboard
taskTitle.IsEnabled = false;
taskTitle.IsEnabled = true;
taskDetail.IsEnabled = true;
}
}
But I cannot access itemGrid, taskTitle, or taskDetail for this specific item. And I have no idea how to pass them to the tapTimer_Tick function.
I have been able to use the Tag="{binding itemID}" on elements, but that still hasn't allowed me to solve this issue.
How do I find the grid item2Expanded that the Tap originated from, and then access elements in the same grid by name?
If I want to access the same element as was clicked, then it's easy:
private void taskTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox taskTitle = (TextBox)sender;
taskTitle.IsEnabled = false;
}
I've been trying to work out how to use Visual Tree Helper to solve this problem, but I have no idea how to do it.