Context menu paste in textbox not updated binding element value - c#

I'm binding a text box to a textblock, but it is not updated when I paste something using the context menu.
Following there is the XAML code for element binding:
<uc:CustomTextBox x:Name="txtBoxLastName"
Grid.Row="3"
Grid.Column="1"
Width="80"
Height="25"
HorizontalAlignment="Left" />
<TextBlock Grid.Row="4"
Grid.Column="1"
Width="100"
Height="100"
Text="{Binding Text,
ElementName=txtBoxLastName}" />
Context menu paste code:
this.SelectedText = Clipboard.GetText();
What's wrong with this code? Is there any other way to do the same ?
Regards.

Using the common Silverlight controls, the text pasted in the TextBox control is automatically pasted also in the TextBlock control.
I think the problem is in the code you're using to paste the text stored in the Clipboard, because you're setting the property SelectedText, when instead your TextBlock's Text property is bound to the Text property of the TextBox.
You can change the line from:
this.SelectedText = Clipboard.GetText();
to:
this.Text = Clipboard.GetText();
or, as second option, change the binding in your textblock from this:
Text="{Binding Text, ElementName=txtBoxLastName}"
to this:
Text="{Binding SelectedText, ElementName=txtBoxLastName}"

Related

Binding label to selected listbox value

Relatively new to WPF and binding. I have a Listbox filled with values. When a user selects a certain value I want to display details of that value over several labels and a textbox. The current LINQ query that fills my listbox list gets the whole table, so the data is inside that list. How do I go around passing the details of the selected value inside of a label?
My current code for the listbox is :
<ListBox x:Name="LBController" Grid.Column="1" HorizontalAlignment="Left" Grid.RowSpan="6" Grid.Row="1"
ItemsSource ="{Binding AllControllers}" SelectedValue="{Binding SelectedControllerID}"
SelectedValuePath="Id" DisplayMemberPath="Name" >
</ListBox>
If a value inside of the listbox is selected, I would like its name to be displayed on a seperate label
<Label x:Name="lbl_controllername"
Content="Controller Naam" HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="1" Grid.Column="2" FontFamily="Corsiva" FontSize="11" Margin="40,0,0,0"/>
EDIT : Thanks for the answers everyone. This does however seem to prove difficult when doing the same thing with a control that has a function behind it.
<TextBox x:Name="txt_CodeDetail"
TextWrapping="Wrap"
AcceptsReturn="True"
FontFamily="Corsiva"
FontSize="11"
Text="{Binding ControllerCode, Mode=TwoWay}"/>
For example I have a textbox, the text inside of it is converted into a string for a function the app does. This string is called ControllerCode. This was done by copy-pasting the code before but I want the textbox to be filled with a selected property from the value inside the listbox.
I can't use
Text="{Binding SelectedItem.Property Elementname=LBController}"
as that would get rid of Controllercode and ruin the function. How can I bind the textbox text to the selecteditem as well as maintain the string that is formed with controllercode?
If you need to display several properties of an element in some area of the window, then it is easier in this area to set a panel in which the data context is bound to the selected element.
And inside this panel, set a set of elements to represent the properties of the selected element.
Example:
<StackPanel DataContext="{Binding SelectedItem, ElementName=LBController}">
<TextBlock Text="{Binding FirstProperty}"/>
<TextBlock Text="{Binding SecondProperty}"/>
<!--Other elements-->
</StackPanel>

WPF : Text loaded in a Wpf textbox is cutted ( not entire )

I'm trying to load a text ( as a string ) in a wpf textbox.
string str;
string = myFunction();
textBox.Text = str;
Here's my XAML :
<TextBox Height="174" Name="textBox1" Width="811" TextWrapping="Wrap" />
My TextBox result is not entire. I trye to write the str in a file on my disk, and the file contains all my data.
Anyone have some idea ?
Thanks a lot,
You've constrained the size of the TextBox, so if you have too much text you won't be able to see it all.
Remove the constraints, or specify a VerticalScrollBarVisibility value:
<TextBox Height="174" Name="textBox1" Width="811" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />

C# store app. Add "X"button at the end of a textbox or searchbox

I have a C# store app where I have a search box or a textbox. I noticed that when I enter text in either the text or searchbox I don't get the "X" button at the end of the box to clear the text.
In this example there is someone with a similar problem: Remove "X" button at the end of a TextBox But I want to reverse that action.
As I understand in the above link, the button should appear automatically, but it does not. Does anyone know how I can get that "X" button to appear on either a searchbox or textbox?
the boxes:
<TextBox
x:Name="textboxNoX"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="401,66,0,0"
TextWrapping="Wrap"
Text=""
VerticalAlignment="Top"
Width="259"/>
<SearchBox
x:Name="searchbarNoX"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="400.998,33.478,0,0"
VerticalAlignment="Top"
RenderTransformOrigin="0.5,0.5"
UseLayoutRounding="False"
Width="256.565"
d:LayoutRounding="Auto">
Ah the property TextWrapping="Wrap" should be set to TextWrapping="NoWrap".
Removing the TextWrapping property from the TextBox XAML worked for me.

How to group together Textblock and Textbox

I am making the UIs for entering master data for various business entities (customer, etc). I run into this need to group together a TextBlock and a TextBox together frequently. i.e.
<Label Content="Civil Status:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="civilStatusTextBox" Text="{Binding Path=CivilStatus, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
<Label Content="Company:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="3" Name="companyTextBox" Text="{Binding Path=Company, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
Is there any way to do this with less typing? i.e.
<custom:LabeledTextBox Label="Civil Status:" Text="{Binding Path=CivilStatus, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" ... />
Or possibly, any libs which offer something like this?
EDIT : Forget the container Grid for a moment and assume it is a StackPanel.
Here's a step-by-step solution that I managed to put together. To set the stage, I'm going to assume we've got a very simple UserControl that has the following XAML content.
<UserControl x:Class="WpfApplication2.UserControl1" [ ... auto gen code removed ... ] >
<TextBox MinWidth="50" x:Name="TBox" />
</UserControl>
From an XAML that uses our UserControl we'd essentially want to set a data binding for the Text property on TBox. Idealy we could use a plain syntax like:
<local:UserControl1 TBox.Text="{Binding ...}" />
unfortunately I don't know any XAML syntax that would allow targeting an sub-element's property, so the next best thing would be to introduce a "staging" property in the UserControl itself and bind through that.
Since Binding only works for Dependency properties, the property we'll introduce needs to be a DependencyProperty. We'll also bind the Text property of TBox straight to our DependencyProperty from code.
The code-behind for the UserControl looks like this:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
// Set binding from code
this.TBox.DataContext = this;
this.TBox.SetBinding(TextBox.TextProperty, new Binding { Path = new PropertyPath("TBValue"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
}
public static readonly DependencyProperty TBValueProperty = DependencyProperty.Register("TBValue", typeof(string), typeof(UserControl1));
public string TBValue
{
get { return this.GetValue(TBValueProperty) as string; }
set { this.SetValue(TBValueProperty, value); }
}
}
}
With this in place we can use the UserControl like this, binding to the TBValue property:
<local:UserControl1 TBValue="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
What you want to achieve (Master-Detail-Views) is actually well supported by Visual Studio out of the box. Open the following Menu structure : Project -> Add Data Source, then choose data source type Object. In the following, select the classes that you want to generate input fields for and finish the wizard.
Then, if not already open, open up your Data Sources tool window (Shift+Alt+D). You should see a DataSource of the type you just generated. To get a labelled field for each property of the object, open the source dropdown and click Details.
Note that the properties have such dropdowns as well, so that you can choose how you want to edit them (ComboBox, TextBox, Custom, no editor,...).
Now just drag the DataSource onto your window. You will get a Grid that's filled with all the labels and editors you desired. DataBinding and validation is also supported right away, so all you will have to do is set the generated Grid's DataContext.
Hope this saves you some work.
P.S. The screenshots are made in my german VS instance, still I thought they might help you identify the right dialogues / windows.

How to get textblock binding value?

I had bind my textblock in xaml, is it possible to get the value out into my coding?
My coding for binding
<TextBlock Height="40" HorizontalAlignment="Left" Margin="8,24,10,0" Name="txtBlockCustName" Text="{Binding CustName, Mode=OneWay}" VerticalAlignment="Top" FontSize="26" />
I want to put in my mainpage.xaml.cs like
string CustName = txtBlockCustName.Text;
but it had error on it..
You can't access this textblock because it is bound in a listboxtemplate. If there are multiple textblocks in the list, how can you access it by name? The program won't know what textblock you are asking for. This is why an error is thrown.
You could use the collection that you bound to the listbox to get the customer name.

Categories