Reputation: 335
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>
Upvotes: 0
Views: 737
Reputation: 381
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.
Upvotes: 1