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.
Related
I just want to make things clear about databinding, so I try things. I want to change contents of some dependency objects dinamically using with binding methods.
Basically what I try to accomplish is writing something in datePickerApp; and show it in richTextBoxComplaint dinamically. (with Button_CLick_2)
It kinda works for the first time I call the property; but for the second time it is not get updated
When I debug the code, beside the first click, I see that it sets the corresponding value but not going inside of the object and calling get for the particular value that I want to see.
I tried to add DataContext=this; to my main function and UpdateSourceTrigger values to my binding objects but still doesn't work.
Here is my XAML code:
<GroupBox Header="Randevu Paneli" Height="233" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="208" DataContext="{Binding Appointment, Mode=TwoWay, ElementName=ViewRoot}">
<StackPanel HorizontalAlignment="Stretch" Height ="210" VerticalAlignment="Bottom" Orientation="Horizontal">
<StackPanel Width="55" Height="190" VerticalAlignment="Bottom" Orientation="Vertical">
<Label Height="24" HorizontalAlignment="Right">Branş:</Label>
<Label Height="24" HorizontalAlignment="Right">Doktor:</Label>
<Label Height="24" HorizontalAlignment="Right">Tarih:</Label>
<Label Height="24" HorizontalAlignment="Right">Saat:</Label>
<Label Height="24" HorizontalAlignment="Right">Şikayet:</Label>
<StackPanel Height="45"></StackPanel>
<Button Height="25" Click="Button_Click" VerticalAlignment="Bottom">Çıkış</Button>
</StackPanel>
<StackPanel Height="185" Width="130" Orientation="Vertical" VerticalAlignment="Bottom">
<ComboBox x:Name="comboBranch" Width="115" SelectionChanged="ComboBox_SelectionChanged"></ComboBox>
<ComboBox x:Name="comboDr" Width="115"></ComboBox>
<DatePicker x:Name="datePickerApp" Width="115" Text="{Binding Date}"></DatePicker>
<ComboBox x:Name="timePickerApp" Width="115" SelectedItem="{Binding Hour}"></ComboBox>
<RichTextBox x:Name="richTextBoxComplaint" Width="115" Height="44">
<FlowDocument>
<Paragraph>
<Run Text="{Binding Date}"></Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Width="115" Height="25" Click="Button_Click_2">Randevu Talep Et</Button>
<Button Width="115" Height="25" Click="Button_Click_1">Bilgilerini Düzenle</Button>
</StackPanel>
</StackPanel>
</GroupBox>
And my .cs with corresponding areas:
Appointments _appointment;
public Appointments Appointment
{
get
{
return _appointment;
}
set
{
_appointment = value;
OnPropertyChanged("Appointment");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
app.Date = datePickerApp.Text;
Appointment = app;
}
As I said it works for the first click but that's it. It shows the old value for other tries. Somehow get doesnt work properly; it works for the object itself but not for the 'Date' variable in particular.
When I debug the code in first click, I see that it goes to 'Appointments' class and call get 'Date' in particular. But it doesnt work the same way for other clicks.
What could I missing?
The following code can be used if the value buttockno in Table Al is of a string type.
What should I do if the value of buttockno in table Al is numeric?
<Window x:Class="DamDarYar_IDMS.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DamDarYar_IDMS"
mc:Ignorable="d"
Title="Window4" Height="300" Width="300">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="151,44,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="60,82,0,0" VerticalAlignment="Top" Height="112" Width="211"/>
</Grid>
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
IDMS_dbEntities db = new IDMS_dbEntities();
try
{
dataGrid.ItemsSource = db.tblAIs.Where(a => a.ButtockNo.StartsWith(textBox.Text.Trim())).ToList();
}
catch
{
}
}
}
}
I would recommend using a filter for that but using your method simply convert to string first.
a.NumericProperty.ToString().StartsWith(...
I'm a relative beginner with WPF so please bear with me. I have a simple app that converts farenheit values to celcius and vice versa. I thought I would have a play with refactoring this to MVVM so I moved everything from my code-behind to a separate class and then set the dataContext programmatically. However I'm getting a lot of ..'does not exist in context errors'. Where am I going wrong? Thanks
XAML
<Window x:Class="FarenheitCelciusConverter.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Temperature Converter" Height="500" Width="500"
xmlns:local="clr-namespace:FarenheitCelciusConverter">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="473" Width="488">
<Label Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lblF" VerticalAlignment="Top" Width="64" FontWeight="Bold">Farenheit</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="10,42,0,0" Name="lblC" VerticalAlignment="Top" Width="64" FontWeight="Bold">Celcius</Label>
<TextBox Height="23" Margin="94,10,112,0" Name="tbFaren" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" />
<TextBox Height="23" Margin="94,42,112,0" Name="tbCelcius" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" />
<Button Margin="94,76,109,0" Name="btnConvert" Click="btnConvert_Click" Height="23" VerticalAlignment="Top" HorizontalContentAlignment="Center" Width="72" HorizontalAlignment="Left">Convert</Button>
<Image Name="image1" Stretch="Fill" Margin="94,112,240,228">
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="C:\Users\Winston\Pictures\thermometer.jpg"/>
</Image.Source>
</Image>
<TextBlock FontWeight="Bold" Height="21" Margin="195,12,173,0" Name="tblCelci" VerticalAlignment="Top" /><TextBlock FontWeight="Bold" Height="21" Margin="195,44,0,0" Name="tblFarenh" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" /><TextBlock FontWeight="Bold" Height="21" Margin="195,78,15,0" Name="tblCex" VerticalAlignment="Top" Foreground="Red" />
</Grid>
</Window>
Code behind
namespace FarenheitCelciusConverter
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new ConverterViewModel();
}
}
}
View Model
namespace FarenheitCelciusConverter
{
public class ConverterViewModel
{
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
tblCex.Text = "";
try
{
if (tbCelcius.Text.Length != 0)
{
double celcius = Double.Parse(tbCelcius.Text);
if (celcius < 99999.0 && celcius > -99999.0)
{
tblFarenh.Text = Math.Round(1.8 * celcius + 32.0) + " F";
}
else
{
throw new OverflowException("Number limit exceeded!");
}
}
if (tbFaren.Text.Length != 0)
{
double farenh = Double.Parse(tbFaren.Text);
if (farenh < 99999.0 && farenh > -99999.0)
{
tblCelci.Text = Math.Round(0.555 * (farenh - 32.0)) + " C";
}
else
{
throw new OverflowException("Number limit exceeded!");
}
}
}
catch (Exception ex)
{
tblCex.Text = ex.Message;
}
}
}
}
When using MVVM data is passed back and forth from the View (Window1) and the ViewModel through Databinding. So each of your textboxes should be databound to a public property in your Viewmodel:
<TextBox Height="23" Margin="94,10,112,0" Name="tbFaren" VerticalAlignment="Top" Width="72" HorizontalAlignment="Left" Text="{Binding FahrenText}"/>
And your ViewModel will take the values in the properties, do something with them, and set the properties bound to the appropriate textboxes.
In this way, the Viewmodel is performing the logic and the View is interpreting the output according to the rules that you are giving it. Later on, you can change the rules in the View without messing with the ViewModel whereas using the codebehind often you have to explicitly set View settings alongside the program logic.
Also, be sure to implement iNotifyPropertyChanged on your ViewModel , otherwise the UI wont know when the databound property values have changed and it wont update. Check out this post for an example.
Also here is the MSDN article on Databinding in WPF.
I'm doing some drag and drop between two ListBox items containing a UserControl, ClaimSectionTemplate as the DataTemplate for the items in the collection that are populating the source listbox.
Now I have 2 buttons on ClaimSectionTemplate, AddField and RemoveField, and they respectively add and remove fields from a child collection on the ClaimSection object that is shown in the ClaimSectionTemplate user control.
So what is happening is when I drop a ClaimSection into the target ListBox the original object becomes unresponsive no longer allowing me to interact with the user control.
MainWindow.Xaml
<ListBox Margin="13,12,12,12" Name="NewSections" Grid.Column="1" AllowDrop="True" Drop="NewSections_Drop">
<ListBox.ItemTemplate>
<DataTemplate>
<me:ClaimSectionTemplate />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Main Window Drag Drop Handlers
private void ExistingSections_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var parent = (ListBox)sender;
dragSource = parent;
var data = ExistingSections.SelectedItem;
if (data != null)
{
DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
}
}
private void NewSections_Drop(object sender, DragEventArgs e)
{
Models.ClaimSection dropData = (Models.ClaimSection)e.Data.GetData(typeof(Models.ClaimSection));
ClaimSectionsNew.addClaimSection(dropData);
ClaimSectionsExisting.removeClaimSection(dropData);
}
ClaimSectionTemplate.xaml
<UserControl x:Class="InsuranceBuildVer1.Views.ClaimSectionTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="187" d:DesignWidth="300">
<Grid Height="185">
<TextBlock Height="23" Margin="12,12,12,0" Name="textBlock1" Text="{Binding Path=ClaimType}" VerticalAlignment="Top" />
<ListBox x:Name="FieldList" HorizontalAlignment="Left" Margin="10,71,0,12" Width="278" ItemsSource="{Binding Path=Fields.ClaimFields, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Identifier}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Add Field" Height="23" HorizontalAlignment="Left" Margin="12,42,0,0" Name="AddField" VerticalAlignment="Top" Width="75" Click="AddField_Click" />
<Button Content="Remove Field" Height="23" HorizontalAlignment="Left" Margin="103,42,0,0" Name="RemoveField" VerticalAlignment="Top" Width="96" Click="RemoveField_Click" />
</Grid>
</UserControl>
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"