I have several GroupBox controls, and all but one work fine. Code is the same, I don't see what the problem might be. I get NullReferenceException. Does anyone have any clue? This is XAML:
<GroupBox Header="Load neural network" Height="90" HorizontalAlignment="Left" Margin="416,34,0,0" Name="groupBox3" VerticalAlignment="Top" Width="207" IsEnabled="False">
<Grid>
<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="114,32,0,0" Name="loadBTN" VerticalAlignment="Top" Width="75" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="6,32,0,0" Name="textBox1" VerticalAlignment="Top" Width="93" />
</Grid>
</GroupBox>
And this is code behind where error pops up:
private void loadRBcheck_Checked(object sender, RoutedEventArgs e)
{
newRBcheck.IsChecked = false;
groupBox1.IsEnabled = false;
groupBox2.IsEnabled = false;
inputTB.IsEnabled = false;
hiddenTB.IsEnabled = false;
outputTB.IsEnabled = false;
groupBox3.IsEnabled = true; //this line causes error
label1.IsEnabled = false;
label2.IsEnabled = false;
label3.IsEnabled = false;
}
Related
I have an image that is inside of a tab item:
<TabItem x:Name="tabThreeTb" Header="Photos" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="55" Margin="1,0,-1,0">
<Grid x:Name="tabThreeBdy" Background="#FFE5E5E5">
<Rectangle Fill="#FFE5E5E5" HorizontalAlignment="Left" Height="369" Margin="12,13,0,0" Stroke="Black" VerticalAlignment="Top" Width="467">
<Rectangle.Effect>
<DropShadowEffect/>
</Rectangle.Effect>
</Rectangle>
<TextBox x:Name="picNotesTextBox" HorizontalAlignment="Left" Height="415" Margin="498,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="299"/>
<Button x:Name="nxtPhotoBtn" Content="Next" HorizontalAlignment="Left" Margin="404,403,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="prevPhotoBtn" Content="Prev" HorizontalAlignment="Left" Margin="10,403,0,0" VerticalAlignment="Top" Width="75"/>
<Label x:Name="photoNumLbl" Content="1 of 4" HorizontalAlignment="Left" Margin="226,401,0,0" VerticalAlignment="Top" Width="42"/>
<Image x:Name="photoTabImage" HorizontalAlignment="Left" Height="369" Margin="12,13,0,0" VerticalAlignment="Top" Width="467" AllowDrop="True" DragEnter="photoTabImage_DragEnter"/>
</Grid>
</TabItem>
I am trying to use drag and drop to allow for the addition of photos to to the list that contains paths for the image source, though I can't seem to be able to fire the DragEnter routine...
I would like the drag and drop functionality to only be alive when the content is being dragged over the Image bounds.
Is there something I need to do for an Item that is nested in a tab control to allow this?
The issue is in that your app for some reason can't proceed standard events set. To fix that switch it to the tunneling model, by simply replacing your events on Preview versions (for instance replace DragEnter="photoTabImage_DragEnter" on to the PreviewDragEnter="photoTabImage_DragEnter")
Best regards,
Maks!
try add
<Label HorizontalAlignment="{Binding ElementName=photoTabImage, Path=HorizontalAlignment}"
Height="{Binding ElementName=photoTabImage, Path=Height}"
Width="{Binding ElementName=photoTabImage, Path=Width}"
Margin="{Binding ElementName=photoTabImage, Path=Margin}"
VerticalAlignment="{Binding ElementName=photoTabImage, Path=VerticalAlignment}"
AllowDrop="True" Drop="ContainerDrop" DragOver="ContainerDragOver"/>
after Image, and use event DragOver
if you need analyze drop object then add next code inside you class
private void ContainerDrop(object sender, DragEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string format in e.Data.GetFormats())
{
sb.AppendLine("Format:" + format);
try
{
object data = e.Data.GetData(format);
sb.AppendLine("Type:" + (data == null ? "[null]" : data.GetType().ToString()));
sb.AppendLine("Data:" + data.ToString());
}
catch (Exception ex)
{
sb.AppendLine("!!CRASH!! " + ex.Message);
}
sb.AppendLine("=====================================================");
}
Console.WriteLine(sb.ToString());
}
private void ContainerDragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
In this part of the code is the event TextChanged to enable the button in te applicationbar.
C#:
private void Textbox_TextChanged(object sender, EventArgs e)
{
ApplicationBarIconButton btn_guardar = ApplicationBar.Buttons[0] as applicationBarIconButton;
if (!string.IsNullOrEmpty(txt_nom_usuario.Text) && !string.IsNullOrEmpty(txt_edad_usuario.Text) && !string.IsNullOrEmpty(txt_peso_usuario.Text))
{
btn_guardar.IsEnabled = true;
}
else
btn_guardar.IsEnabled = false;
}
XAML:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar Mode="Default" IsVisible="True">
<shell:ApplicationBarIconButton x:Name="btn_guardar" IconUri="/icons/appbar.save.rest.png" Text="Guardar" Click="btn_guardar_Click" IsEnabled="False" />
<shell:ApplicationBarIconButton x:Name="btn_atras" IconUri="/icons/appbar.back.rest.png" Text="AtrĂ¡s" Click="btn_atras_Click" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
<TextBlock x:Name="lbl_ingresanombre" Height="39" Margin="60,28,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Width="248" FontSize="29.333" FontFamily="{StaticResource Helvetica}"><Run Text="Ingresa "/><Run Text="tu nombre"/></TextBlock>
<TextBox x:Name="txt_nom_usuario" Height="63" Margin="47,58,69,0" TextWrapping="Wrap" Text="
" FontSize="21.333" VerticalAlignment="Top" IsEnabled="True" />
<TextBlock x:Name="lbl_edad" Height="38" Margin="60,117,0,0" TextWrapping="Wrap" Text="Ingresa tu edad" VerticalAlignment="Top" FontSize="29.333" HorizontalAlignment="Left" FontFamily="{StaticResource Helvetica}"/>
<TextBox x:Name="txt_edad_usuario" InputScope="TelephoneLocalNumber" Height="63" TextWrapping="Wrap" Text="
" FontSize="21.333" Margin="47,147,69,0" VerticalAlignment="Top" MaxLength="3" />
<TextBlock x:Name="lbl_peso" Height="42" Margin="60,0,0,178" TextWrapping="Wrap" Text="Peso" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="74" FontSize="29.333" d:LayoutOverrides="HorizontalAlignment" FontFamily="{StaticResource Helvetica}"/>
<TextBox x:Name="txt_peso_usuario" InputScope="TelephoneLocalNumber" Margin="47,0,69,125" TextWrapping="Wrap" Text="
" FontSize="21.333" Height="63" VerticalAlignment="Bottom"/>
The application bar doesn't support some basic features when it is set in XAML. You'll have to create the bar and buttons and/or menu items through code.
Here's an example how you can create the bar and add controls to it. The controls can then be accessed later from code:
//button
var appBarButton = new ApplicationBarIconButton
{
IconUri = new Uri("/Images/YourImage.png", UriKind.Relative),
Text = "click me"
};
appBarButton.Click += new EventHandler(appBarButton_Click);
//menu item
ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem
{
Text = "a menu item"
}
appBarMenuItem.Click += new EventHandler(appBarMenuItem_Click);
//application bar
//Note that this is not a variable declaration
//'ApplicationBar' is a property of 'PhoneApplicationPage'
ApplicationBar = new ApplicationBar();
ApplicationBar.Buttons.Add(appBarButton);
ApplicationBar.MenuItems.Add(appBarMenuItem);
//the events
private void appBarButton_Click(object sender, EventArgs e)
{
}
private void appBarMenuItem_Click(object sender, EventArgs e)
{
}
When all this is done, you've created your own ApplicationBar through code. Now you can change the properties from code, like this:
var theButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
if(someCondition)
{
theButton.IsEnabled = true;
}
else
{
theButton.IsEnabled = false;
}
//or shorter:
theButton.IsEnabled = someCondition
This is just an example. In the TextChanged events you can also access the ApplicationBar controls. In these events you can place above code to change the ApplicationBarButton. Hope this clears things up for you! More reading and info:
ApplicationBar Class
PhoneApplicationPage.ApplicationBar Property
How to change app bar icon buttons and menu items dynamically
I want to load a rtf file in silverlight using RichTextBox control but this control doesn't load file in proper format. can anyone tell how to do so or convert rtf to xaml?
Here's the xaml :
<Grid x:Name="LayoutRoot" Height="480" Width="640">
<RichTextBox HorizontalAlignment="Left" Margin="89,64,0,0" Name="rtb" VerticalAlignment="Top" Height="301" Width="416"/>
<Button Content="Select File" Height="23" HorizontalAlignment="Left" Margin="268,12,0,0" Name="btnSelect" VerticalAlignment="Top" Width="75" Click="btnSelect_Click" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="FileName:" VerticalAlignment="Top" Width="73" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="89,12,0,0" Name="txtFile" VerticalAlignment="Top" Width="164" />
<Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="430,385,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" Click="btnReset_Click" IsEnabled="False" />
</Grid>
Code behind :
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog odlg = new OpenFileDialog();
odlg.Filter = "Text files|*.rtf";
odlg.Multiselect = false;
string contents = null;
if ((bool)odlg.ShowDialog())
{
txtFile.Text = odlg.File.Name;
StreamReader reader = new StreamReader(odlg.File.OpenRead());
while (!reader.EndOfStream)
{
contents = reader.ReadToEnd();
}
reader.Close();
rtb.Selection.Text = contents;
btnReset.IsEnabled = true;
}
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
txtFile.Text = "";
rtb.Blocks.Clear();
}
hi i need a function for WPF application is similar to the autopostback option in asp.net. what happens is i have a form with a combobox populated by xml file. so once user select "Others" as an option, a textbox and a button becomes visible for them. so is there anything wrong with my code? i used the compare string method to implement the function but seems like it is not working.
private void comboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
if (comboBox1.SelectedValue.ToString() == "Others")
{
BuilderemailTextBox.Visibility = Visibility.Visible;
BuilderupdateButton.Visibility= Visibility.Visible;
}
else
{
BuilderemailTextBox.Visibility = Visibility.Hidden;
BuilderupdateButton.Visibility = Visibility.Hidden;
}
}
i also tried the following, making changes to SelectedIndex too.
private void comboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
if (comboBox1.SelectedIndex.ToString() == "Others")
{
BuilderemailTextBox.Visibility = Visibility.Visible;
BuilderupdateButton.Visibility= Visibility.Visible;
}
else
{
BuilderemailTextBox.Visibility = Visibility.Hidden;
BuilderupdateButton.Visibility = Visibility.Hidden;
}
}
edit 1: My XAML file:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="309" Width="672">
<Grid>
<Button Height="23" Name="BuildButton" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Click="BuildButton_Click">Build</Button>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,63,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectedIndexChanged" />
<CheckBox Height="16" HorizontalAlignment="Left" Margin="66,0,0,140" Name="ExecbuildstartingmailCheckBox" VerticalAlignment="Bottom" Width="153">Exec Build Starting Mail</CheckBox>
<ComboBox Height="23" Margin="0,63,173,0" Name="comboBox2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
<Button Height="23" Margin="270,63,0,0" Name="BuilderupdateButton" VerticalAlignment="Top" HorizontalAlignment="Left" Width="51" Visibility="Hidden">Button</Button>
<Button Height="23" Margin="0,63,73,0" Name="button2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="74">Button</Button>
<TextBox Height="23" HorizontalAlignment="Left" Margin="144,63,0,0" Name="BuilderemailTextBox" VerticalAlignment="Top" Width="120" Visibility="Hidden" />
</Grid>
</Window>
edit2 my xml file:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<value>builder#example.com</value>
</builder>
<builder>
<value>Others</value>
</builder>
</builderemail>
<manageremail>
<manager>
<value>manager#example.com</value>
</manager>
<manager>
<value>Others</value>
</manager>
</manageremail>
</email>
Depending on your XML you can take the SelectedItem, cast it and access its properties. If your item has an XmlElement with the name value, which you want to test for "Other" you could try this:
(comboBox1.SelectedItem as XmlElement).GetElementsByTagName("value")[0].InnerText == "Other"
I have this simple xaml page to collect user information:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="50" HorizontalAlignment="Left" Margin="6,36,0,0" Text="Enter your birth year :" VerticalAlignment="Top" Width="312" FontSize="32" />
<TextBox FontSize="32" Height="84" HorizontalAlignment="Left" Margin="310,19,0,0" Name="birthyear" Text="" VerticalAlignment="Top" Width="146" TextAlignment="Center" />
<TextBlock Height="60" HorizontalAlignment="Left" Margin="12,112,0,0" Name="birthyear_message" Text="" VerticalAlignment="Top" Width="438" />
<TextBlock FontSize="32" Height="50" HorizontalAlignment="Left" Margin="6,283,0,0" Text="Enter your post code :" VerticalAlignment="Top" Width="312" />
<TextBox FontSize="32" TextAlignment="Center" Height="82" HorizontalAlignment="Left" Margin="310,268,0,0" Name="postcode" Text="" VerticalAlignment="Top" Width="146" />
<TextBlock Height="60" HorizontalAlignment="Left" Margin="12,356,0,0" Name="postcode_message" Text="" VerticalAlignment="Top" Width="438" />
<Button Content="Cancel" Height="72" HorizontalAlignment="Left" Margin="31,441,0,0" Name="cancel" VerticalAlignment="Top" Width="160" Click="cancel_Click" />
<Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="247,441,0,0" Name="save" VerticalAlignment="Top" Width="160" Click="save_Click" />
</Grid>
My code behind is like this:
//Set and display user a set of default values if user is accessing page for first time else retrieve data from the settings
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("birthyear"))
birthyear.Text = settings["birthyear"].ToString();
else
{
birthyear.Text = (DateTime.Now.Year - 25).ToString();
settings.Add("birthyear", birthyear.Text);
}
if (settings.Contains("postcode"))
birthyear.Text = settings["postcode"].ToString();
else
{
postcode.Text = "10044";
settings.Add("postcode", postcode.Text);
}
}
private void save_Click(object sender, RoutedEventArgs e)
{
var settings = IsolatedStorageSettings.ApplicationSettings;
//ignore error conditions for the time being!
settings.Remove("birthyear");
settings.Add("birthyear", birthyear.Text);
settings.Remove("postcode");
settings.Add("postcode", postcode.Text);
settings.Save();
//Navigate to the same page to validate values entered by user is saved
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
Now coming to the problem. When the user navigates for the first time, the default values of 1986 and 10044 are shown. Now I edit the values, say 1999 and 65222 and select Save. What happens now is that the birthyear is showing 65222 and postcode is showing blank. What is the problem?
Update:
When I try to retrieve the values in the Intermediate Window in VS, the new values are displayed properly. I am guessing it is saving properly but is having problems while retrieving/displaying it
You could try changeing the value instead, and do
settings["birthyear"] = birthyear.Text;
settings["postcode"] = postcode.Text;
And maybe also try casting your settings as type IsolatedStorageSettings.
EDIT:
settings.Remove("birthyear");
settings.Add("birthyear", birthyear.Text);
settings.Remove("postcode");
settings.Add("postcode", postcode.Text);
settings.Save();
Becomes:
settings["birthyear"] = birthyear.Text;
settings["postcode"] = postcode.Text;