VB WPF textblock.text equal a text file - wpf

In VB2010 WPF, i have a textblock which, when the window loads, i need the text inside the textblock to be the same as the text in an outside .txt file. How can i do this?
Thanks
Nick

<Window
...
Loaded="Window_Loaded">
...
<TextBlock Name="textBlock" .../>
...
</Window>
and...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
textBlock.Text = System.IO.File.ReadAllText(path);
}

Related

Detect stylus eraser button on WPF

Is there any way to detect that the stylus eraser button is pressed on a WPF element?
The answer seems to be to detect if the pen is Inverted.
http://msdn.microsoft.com/EN-US/library/ms607230(VS.110).aspx
(tested on Lenovo x61 tablet)
In MainWindow.Xaml
<TextBlock Name="textBox"
Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center"
StylusDown="TextBlock_StylusDown_1"/>
In MainWindow.Xaml.cs
private void TextBlock_StylusDown_1(object sender, StylusDownEventArgs e)
{
if (e.Inverted)
textBox.Text = "I've been erased";
else
textBox.Text = "Normal stylus";
}

focussing cursor in textbox using xaml in wpf

private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
txt1_focus.Focus();
}
How can i achieve the above code using using xaml file in wpf.
use the FocusManager.FocusedElementlike below:
<Window x:Class="UI.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusManager.FocusedElement="{Binding ElementName=txtSearch}">
</Window>
http://cloudstore.blogspot.com/2008/06/setting-initial-focus-in-wpf.html
This site explains how to set the initial focus on a certain control.
<Window ...
FocusManager.FocusedElement="{Binding ElementName=TextBox1}">

How to make TextChanged happen for copy/paste for TextBox in Silverlight?

For silverlight control TextBox, when copy/paste content for TextBox Text property, TextChanged Event not happening.
How to make paste same as typing for Text in TextBox?
Sorry, I can't reproduce this.
I used the following XAML
<StackPanel>
<TextBox TextChanged="TextBox_TextChanged" />
<TextBlock x:Name="tbk" />
</StackPanel>
and the following event handler
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
tbk.Text += "X";
}
Every time I typed in the TextBox or pasted text into it, the TextBlock gained another X.

databinding in silverlight

I have a xaml with a button like this:
Button.xaml
<Grid x:Name="LayoutRoot" >
<StackPanel >
<Button Content="Button1" Click="Button1_Click" />
</StackPanel >
</Grid>
and Button.xaml.cs:
private void Button1_Click(object sender, RoutedEventArgs e)
{
// Get a instance of ClientOversikt
CustomerView childWindow = m_container.Resolve<CustomerView >();
childWindow.Show();
}
It's working fine. But I want to use Databinding in Button.xaml instead of Click="Button1_Click". How could I do it?
I appreciate all the help
Since you're using Silverlight 4, you can use commands. You bind the Command property of the Button to an instance of ICommand, which will open the child window when executed. Then, when you click on the button, the command will be executed.
This page contains a reasonably good introduction to commanding.

How to bind EDM to WPF ListBox?

I'm trying to figure out WPF binding to SQLite.
I have an ADO.NET Entity Data Model generated to represent my SQLite database. The database only holds one table "People" with two columns "person_id" and "person_name". Now, I generated EDM classes for that table within my WPF Application.
I'm trying to bind to a list box. I can delete items from source and see it updating the list box. But I can't add items to the source using a text box and see it update the list box.
I declared data entities in Window1 class like this:
private static MyNewSqliteDbEntities2 _myEntities = new MyNewSqliteDbEntities2();
I have a list box that is bound to the ObjectQuery in the Window_Loaded event handler like this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
peopleListBox.ItemsSource = _myEntities.People;
}
I have another text box which I use to add people by clicking on button. And I can delete Items by selecting an item in the list box and clicking delete button. Changes are committed to the database when a commit button is clicked. Please consider the code below:
private void addButton_Click(object sender, RoutedEventArgs e)
{
if (addPersonTextBox.Text != "")
{
People newPerson = new People();
newPerson.person_name = addPersonTextBox.Text;
//_myEntities.AddToPeople(newPerson);
_myEntities.AddObject("People", newPerson);
addPersonTextBox.Text = "";
}
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
_myEntities.DeleteObject(peopleListBox.SelectedItem);
}
private void commitButton_Click(object sender, RoutedEventArgs e)
{
_myEntities.SaveChanges();
}
I tried refreshing the list box control using another button called "Refresh" in the following manner but with no luck (although, when I step through the code I see the source is updated):
private void refreshButton_Click(object sender, RoutedEventArgs e)
{
peopleListBox.ItemsSource = null;
peopleListBox.ItemsSource = _myEntities.People;
}
Here is the XAML code if you are wondering:
&ltWindow x:Class="BindingToSqLite.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="400" Loaded="Window_Loaded"&gt
&ltWindow.Resources&gt
&ltDataTemplate x:Key="personNameTemplate"&gt
&ltTextBlock Text="{Binding Path=person_name}"/&gt
&lt/DataTemplate&gt
&lt/Window.Resources&gt
&ltGrid&gt
&ltGrid.ColumnDefinitions&gt
&ltColumnDefinition Width="190*" /&gt
&ltColumnDefinition Width="94*" /&gt
&ltColumnDefinition Width="94*" /&gt
&lt/Grid.ColumnDefinitions&gt
&ltGrid.RowDefinitions&gt
&ltRowDefinition Height="182*" /&gt
&ltRowDefinition Height="38*" /&gt
&ltRowDefinition Height="38*" /&gt
&ltRowDefinition Height="32*" /&gt
&lt/Grid.RowDefinitions&gt
&ltListBox Margin="5" Name="peopleListBox" Grid.ColumnSpan="3" ItemTemplate="{StaticResource personNameTemplate}" /&gt
&ltTextBox Grid.Row="1" Grid.ColumnSpan="2" Margin="5,10" Name="addPersonTextBox" /&gt
&ltButton Grid.Column="2" Grid.Row="1" Margin="5" Name="addButton" Click="addButton_Click"&gtAdd&lt/Button&gt
&ltButton Grid.Row="2" Margin="5" Name="commitButton" Click="commitButton_Click"&gtCommit&lt/Button&gt
&ltButton Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Margin="5" Name="deleteButton" Click="deleteButton_Click"&gtDelete&lt/Button&gt
&ltButton Grid.Row="3" Margin="5" Name="refreshButton" Click="refreshButton_Click"&gtRefresh&lt/Button&gt
&lt/Grid&gt
&lt/Window&gt
I'm not sure if I'm doing this completely wrong. Any help is appreciated.
The People property needs to be an ObservableCollection<T> for this to work.

Resources