Accessing items in DataTemplate using Visual Tree Helper in ListBox - c#

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.

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 hide UWP flyout on a specific condition

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);
}
}

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();

Delete selected item from listbox in windowsphone using c#

i want delete selected item from my listbox using context menu drop-down box here is my xaml
<ListBox Margin="3,60,1,10" Grid.Row="1" Name="lstNews" Tap="lstNews_Tap" Width="476" d:LayoutOverrides="VerticalMargin">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Grid.Row="1" Orientation="Horizontal" Height="120" Width="478">
<StackPanel.Background>
<ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
</StackPanel.Background>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
<toolkit:MenuItem Header="Open" Click="MenuItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid HorizontalAlignment="Left" Width="30" Background="#FF0195D5" Margin="0,0,0,2" Height="118">
<TextBlock x:Name="txtDate" TextWrapping="Wrap" Text="{Binding Path=newsDate}" RenderTransformOrigin="0.5,0.5" Margin="-43.169,44.001,-43.831,0" UseLayoutRounding="False" d:LayoutRounding="Auto" TextAlignment="Center" Height="30" VerticalAlignment="Top" Width="117">
<TextBlock.RenderTransform>
<CompositeTransform Rotation="-90"/>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
<Grid HorizontalAlignment="Left" Width="5" Height="120"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Width="432" Height="114">
<TextBlock x:Name="txtTitle" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsTitle}" Foreground="Black" FontSize="18.667" HorizontalAlignment="Left" Width="432" FontWeight="Bold" />
<StackPanel Orientation="Horizontal" Width="432" Height="27">
<TextBlock x:Name="txtBy" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsSource}" Foreground="Black" FontSize="18.667" Width="399"/>
<Image x:Name="imgArrow" Width="25" Source="Images/Go-In-Arrow.png" Height="25" Margin="5,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Width="433" Height="60">
<TextBlock x:Name="txtDesc" Height="58" TextWrapping="Wrap" Foreground="Black" Text="{Binding Path=newsShortDescription}" FontSize="18.667" Width="371"/>
<TextBlock x:Name="txtID" Height="56" Text="{Binding Path=newsID}" TextWrapping="Wrap" Foreground="Black" FontSize="18.667" Width="8" Visibility="Collapsed"/>
<Image x:Name="imgType" Width="35" Source="{Binding Path=newsTypeImage}" Height="40" Margin="27,20,0,0" d:LayoutOverrides="Height"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
here is what i am trying to do on delete event "MenuItem_Click" but than it throws error "Operation not supported on read-only collection.so what is code to delete selected item on that click event
// this is code to delete i am trying--> lstNews.Items.Remove(lstNews.SelectedItem.ToString());
//below is code im trying to set listboxitemsource
private void FillListBox()
{
fulllist = new nList();
lstNews.ItemsSource = fulllist;
}
public class nList : List<NewsData>
{
StringData sd = new StringData();
public IList<NewsData> GetLCList()
{
IList<NewsData> lcList = null;
using (NewsDataContext context = new NewsDataContext(sd.news_string))
{
IQueryable<NewsData> query = (from app in context.NewsData select app).OrderByDescending(app => app.entryID);
lcList = query.ToList();
}
return lcList;
}
public nList()
{
IList<NewsData> lcData = this.GetLCList();
StringBuilder messageBuilder = new StringBuilder();
foreach (NewsData lc in lcData)
{
Add(new NewsData
{
newsID = lc.newsID,
newsTitle = lc.newsTitle,
newsSource = lc.newsSource,
newsDate = (new GetDate()).getdate(lc.newsDate),//(new AnnouncementList()).getdate(lc.newsDate),
newsShortDescription = lc.newsShortDescription,
newsTypeImage = lc.newsTypeImage,
newsSharing = lc.newsSharing
});
}
}
}
lstNews.Items is list of object that are displayed on the page. So this lstNews.Items is collection of your datatemplate so that is why when you tried lstNews.Items.Remove(lstNews.SelectedItem.ToString()) than this fails.
you should use lstNews.Items.Remove(lstNews.SelectedItem) to delete item.
But for best practice it is prefered to delete item from the source not from the list. i.e. You should delete item from fulllist and reassign it as lstNews.ItemsSource = fulllist;
Changes done in your code
fulllist should be a type of ObservableCollection so that all the changes done on data can be reflected to UI.
To convert List to ObservableCollection Following code can be used:
fulllist = new ObservableCollection<NewsData>(new nList());
Add the implementation for deleting data from fulllist a possible implementation could be:
object obj = lstNews.SelectedItem;
if(obj is NewsData){
fulllist.Remove((NewsData)obj);
lstNews.ItemsSource = fulllist;
}

Disabling bunch of Text Boxes dynamically using a single Check Box

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).

Categories