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"
Related
I have 2 button but they need to be in a different file than the mainWindow.cs.
I can't figure how to do that.
So the Button_Click_2 must be in the ReadData.cs and the
Button_Click_3 must be in the WriteData.cs
The app don't recognize the button when they are not in the mainWindow.
How can I do that ?
ReadData.cs :
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
string text = verifyCard("5"); // 5 - is the block we are reading
textBlock1.Text = text.ToString();
}
MainWindow.xaml.cs :
public void Button_Click_2(object sender, RoutedEventArgs e)
{
//When I click it don't detected the code in the ReadData.cs
// The code of the button must be in the ReadData.cs
}
MainWindow.xaml :
<Grid>
<Button Content="Connexion" HorizontalAlignment="Left"
Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44"
Click="Button_Click_1"/>
<Button Content="Lire donnée carte" HorizontalAlignment="Left"
Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42"
Click="Button_Click_2"/>
<Button Content="Ecrire donnée carte" HorizontalAlignment="Left"
Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41"
Click="Button_Click_3"/>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="23"
Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left"
Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
Height="23" Width="177"/>
</Grid>
It must be possible to forced the app to detected and execute the code in another file than the Main.
I'm stuck... do any of you have the solution ?
You can't just place the button code in ReadData.cs class. as the event is related to ui of MainWindow.xaml create an object for ReadData do something like this
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
ReadData rd= new ReadData();
string text = rd.verifyCard("5");
textBlock1.Text = text.ToString();
}
I've been doing some research, and essentially have come up empty. I would think this is probably easy to figure out, but well beyond my current knowledge. This is regarding the two text boxes.
I would like to have it so I can search for a word in the searchText box and the translated text would appear in the the searchResults text box. (The xml file is located at https://www.dropbox.com/s/jgw84kqj2k1bwq1/JapaneseEnglishData.xml?dl=0. However, at the bottom of the post is a sample of it). Does anyone have any insight on how I could accomplish this?
<Window x:Class="BeginnersJapanese.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="XmlData"
Source="https://www.dropbox.com/s/jgw84kqj2k1bwq1/JapaneseEnglishData.xml?dl=1"
XPath="WordList/Word"/>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource XmlData}}" DisplayMemberPath="English" HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="179"/>
<TextBox Name="searchBox" HorizontalAlignment="Left" Height="23" Margin="256,41,0,0" TextWrapping="Wrap" Text="{Binding Path=WordList/Word,BindsDirectlyToSource=True}" VerticalAlignment="Top" Width="142"/>
<Label Content="SearchBox" HorizontalAlignment="Left" Margin="287,10,0,0" VerticalAlignment="Top"/>
<Button Content="Search" Name="searchButton" HorizontalAlignment="Left" Margin="256,207,0,0" VerticalAlignment="Top" Width="142" Height="36" Click="searchButton_Click"/>
<Button Content="Speak" Name="speakButton" HorizontalAlignment="Left" Margin="256,273,0,0" VerticalAlignment="Top" Width="142" Height="36" Click="speakButton_Click"/>
<TextBox Name="searchResult" HorizontalAlignment="Left" Height="23" Margin="256,162,0,0" TextWrapping="Wrap" Text="{Binding Path=searchBox}" IsReadOnly="True" VerticalAlignment="Top" Width="142"/>
</Grid>
</Window>
A sample of the XML file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--This is a generated XML File-->
<WordList>
<Word>
<English>Me</English>
<Romaji>boku</Romaji>
<Kanji>ぼく</Kanji>
</Word>
<Word>
<English>I</English>
<Romaji>boku</Romaji>
<Kanji>ぼく</Kanji>
</Word>
</WordList>
You need to set the DataContext of your Window or Grid in order to get your binding to work.This might be helpful.
like so:
<Window
xmlns:local="clr-namespace:MyNamespace"
Window>
<Window.Resources>
<local:MyClass x:Key="MyClass">
</Window.Resources>
<Grid DataContext={StaticResource MyClass}>
...
implementation for MyClass:
public class MyClass : INotifyPropertyChanged
{
private string _wordlist;
private string _searchBox;
public string WordList
{
get
{
return _wordlist;
}
set
{
_wordlist = value;
RaisePropertyChanged("WordList");
}
}
public string searchBox
{
get
{
return _searchBox;
}
set
{
_searchBox= value;
RaisePropertyChanged("searchBox");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, propertyName);
}
}
You may need some additional logic inside the setters to translate using the XML.
Also, set the UpdateSourceTrigger for your TextBoxes to PropertyChanged:
<TextBox Text={Binding Path=WordList, UpdatesourceTrigger=PropertyChanged} ... />
<TextBox Text={Binding Path=searchBox, UpdateSourceTrigger=PropertyChanged} ... />
I hope this gives you an idea how to get your desired behavior.
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;
}
I am showing a window. The instance is created and shown within the ViewModel (bad practice I know...)
NewWindow form = new NewWindow();
form.ShowDialog();
Within that form I have an OK_button which is doing stuff when it is pressed. There exist a ViewModel to this form which has the OK Command from the OK_Button.
After that button is pressed doing stuff I want to close that form programatically from within the viewmodel. How can I do that?
I use WPF
UPDATE
now lets see what I do wrong: Here the DataContext event is not fired although my Window with the ViewModel is shown!?
The window that is shown and must be closed from the ViewModel:
public partial class NewSchoolYearWindow : Window
{
public NewSchoolYearWindow()
{
InitializeComponent();
}
private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
NewSchoolYearViewModel vm = (NewSchoolYearViewModel)e.NewValue;
vm.CloseNewSchoolYearDialog += () => this.Close();
}
}
Why is the DataContextChanged even not fired?
I use this XAML in my Window:
<Window x:Class="TBM.View.NewSchoolYearWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:TBM.ViewModel"
Title="Start a new school year"
Height="412" Width="505"
WindowStartupLocation="CenterScreen"
WindowStyle="ThreeDBorderWindow"
ResizeMode="CanResize" DataContextChanged="Window_DataContextChanged">
<Window.Resources>
<ViewModel:NewSchoolYearViewModel x:Key="NewSchoolYearViewModelID" />
</Window.Resources>
<Grid DataContext="{Binding ., Source={StaticResource NewSchoolYearViewModelID}}" Name="MainGrid">
<TextBlock Height="27" HorizontalAlignment="Left" Margin="68,46,0,0" Name="textBlock1" Text="School year start" VerticalAlignment="Top" Width="98" />
<TextBlock Height="27" HorizontalAlignment="Left" Margin="68,93,0,0" Name="textBlock2" Text="School year end" VerticalAlignment="Top" Width="98" />
<TextBlock Height="27" HorizontalAlignment="Left" Margin="68,169,0,0" Name="textBlock4" Text="Database name:" VerticalAlignment="Top" Width="150" TextAlignment="Left" TextTrimming="CharacterEllipsis" />
<TextBlock Height="27" HorizontalAlignment="Left" Margin="68,215,0,0" Name="textBlock3" Text="Directory:" VerticalAlignment="Top" Width="63" TextAlignment="Left" TextTrimming="CharacterEllipsis" />
<TextBox IsReadOnly="True" Text="{Binding CurrentSchoolYear.Directory}" Height="23" HorizontalAlignment="Left" Margin="172,212,0,0" Name="textBox3" VerticalAlignment="Top" Width="224" />
<Button Command="{Binding OpenNewSchoolYearDialogCommand}" Content="DIR" Height="23" HorizontalAlignment="Right" Margin="0,211,27,0" Name="button1" VerticalAlignment="Top" Width="54" />
<Button Command="{Binding CreateNewSchoolYearCommand}" Content="OK" Height="23" HorizontalAlignment="Left" Margin="381,299,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="300,299,0,0" Name="button3" VerticalAlignment="Top" Width="75" />
<DatePicker Height="25" HorizontalAlignment="Left" Margin="172,42,0,0" SelectedDate="{Binding CurrentSchoolYear.Start}" SelectedDateFormat="Long" VerticalAlignment="Top" Width="175" />
<DatePicker Height="25" HorizontalAlignment="Left" Margin="172,89,0,0" SelectedDate="{Binding CurrentSchoolYear.End}" SelectedDateFormat="Long" VerticalAlignment="Top" Width="175" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="172,166,0,0" Name="textBox1" Text="{Binding CurrentSchoolYear.Name}" VerticalAlignment="Top" Width="175" />
</Grid>
</Window>
Declare an event in the ViewModel:
public event EventHandler<CloseRequestedEventArgs> CloseRequested;
protected virtual void OnCloseRequested(bool? dialogResult)
{
var handler = CloseRequested;
if (handler != null)
handler(this, new CloseRequestedEventArgs(dialogResult));
}
...
public class CloseRequestedEventargs : EventArgs
{
private readonly bool? _dialogResult;
public CloseRequestedEventargs(bool? dialogResult)
{
_dialogResult = dialogResult;
}
public bool DialogResult { get { return _dialogResult; } }
}
And handle it in the code-behind:
var vm = (MyViewModel)DataContext;
vm.CloseRequested += vm_CloseRequested;
...
private void vm_CloseRequested(object sender, CloseRequestedEventArgs e)
{
if (e.DialogResult.HasValue)
this.DialogResult = e.DialogResult; // sets the dialog result AND closes the window
else
this.Close();
}
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;