How can I add value to existing textbox in xaml - wpf

I have the following TextBlock:
<TextBlock VerticalAlignment="Vertical" Text="Name" />
This is the TextBlock for my button on which I want to display name and value.
I want to display text on the button as Name[some_value].
I'm getting this this some_value run-time from some function. How can I print the value here?

It seems like you're not yet familiar with Binding in XAML.
You can read, and see examples on MSDN here.

Go to ViewModel of that View:
private string _buttonValue;
public string ButtonValue{
get {return _buttonValue;}
}
Go to constructor and write down:
_buttonValue = "Name " + put_here_SomeValue_Runtime_Value;
Now, to to .xaml
Bind like belw:
<TextBlock Text="{Binding ButtonValue, Mode=OneWay}"/>

Related

Bind TextBlock text when it is more then a simple string

I have a textblock and I would like to bind its content to a property in my viewmodel. This is fine if the content is a simple string. But it's no so fine if I want to format the content and use or tags... In this case I cannot bind a string: the textblock would simply display a string like this "Hallo".
Any ideas ? Thanks
if you have a property of some type - you can create a datatemplate for this type
<DataTemplate DataType="{x:Type local:MySomeType}">
<!--your visual presentation goes here-->
</DataTemplate>
now you can simply use a ContentPresenter to show your property
<ContentPresenter Content="{Binding MySomeTypeProperty}"/>
See what the StringFormat property can do for you. If that is not sufficient, you might want to write a binding converter.
Something like this:
<Textblock content="{Binding MyProperty, StringFormat={}Hello {1}}" />
Just got to play with the string format.

wpf binding to string

I have a Movie class with a Dim _characters = New ObservableCollection(of String)
Characters is the associated property to get and set
How can i get characters to show up in the listBox using Binding?
So far i have the following, this isn't working as i don't know what to put instead of ToString.
<ListBox Name="cList" ItemsSource="{Binding Characters}">
<ItemsControl >
<ItemsControl.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding ToString}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ListBox>
I want them to be editable, hence a textbox.
i tried to bind Characters to TextBox directly, even that didn't work.
Edit :
in the code i have parentGrid1.DataContext = me.movies where
parent grid holds movies.
For those who are experiencing the exception
... binding requires path or xpath ...
You can bind the object directly this way:
<Label Content="{Binding .}" />
Change your TextBox binding to the following. I think it should work:
<TextBox Text="{Binding}"/>
This loads the item itself instead of a property or method output. Since the item is a string it should bind to the strings value.
You cannot perform two-way binding to ObservableCollection<string>. In order to make the strings editable you have to create a class with a string get/set property as the following class Foo:
public class Foo
{
string _text;
public Foo(string text)
{
_text = text;
}
public string Text
{
get { return _text; }
set { _text = value; }
}
}
Your Characters should then be of type ObservableCollection<Foo> and your XAML should be changed so that the textboxes are binding to Foo.Text:
<ListBox ItemsSource="{Binding Characters}" >
<ListBox.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding Text}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Just remove the ToString portion of the code.
Currently you are telling the program that you want to bind to an object called ToString
I take it that Characters is a public property. Debug and be sure that get is being called for Characters. If you have a the datacontext of the page/window to Movies then you need ItemsSource on the ListBox to be {Binding Path=Characters}

WPF TextBlock binding with <LineBreak/>

I have a TextBlock binding as follows in my ControlTemplate.
<TextBlock Grid.Column="1" VerticalAlignment="Center"
FontSize="16" FontFamily="Arial" FontWeight="Bold"
Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ButtonText}">
</TextBlock>
When I set ButtonText as follows with , it doesn't work. It doesn't display in separate line.
ButtonText="Change<LineBreak/> Casette"
How to fix this? Appreciate your help, please provide me with sample code.
A TextBlock displays the contents of its Inlines property. The Text property exists only as a convenience (though it's a significant one): if you set the Text property, the TextBlock will create a Run, set its content to the string you've provided, and save it in the Inlines collection.
When you set the content of a TextBlock element in XAML, the XamlReader populates the Inlines collection directly rather than through the Text property. It parses text nodes into Run objects, and elements as usual for XAML. So this:
<TextBlock>
Line1<LineBreak/>Line2
</TextBlock>
is treated as though it were actually this:
<TextBlock>
<Run>Line1</Run>
<LineBreak/>
<Run>Line2</Run>
</TextBlock>
Note, by the way, that if you try to set the Text property explicitly:
<TextBlock>
<TextBlock.Text>
Line1<LineBreak/>Line2
</TextBlock.Text>
</TextBlock>
you'll get an exception, because the XamlReader will try to create a LineBreak object, and the Text property can only contain a string.
Your binding isn't working the way you want it to because it's explicitly setting the Text property to a string. This doesn't get parsed as XAML (and good thing, too). And so what's displaying in the TextBlock is the content of that string.
So there are basically two ways to accomplish what you're trying to accomplish. In your case, you probably can just get away with embedding a newline into the string.
But this is trickier than it looks if you're doing it from XAML. Because XAML is XML, and XML does some funny things to whitespace. You're OK if you set it explicitly in an attribute using XML character entities, e.g.:
<TextBlock Text="Line 1
Line 2"/>
But that won't work if you do it this way:
<TextBlock>
<TextBlock.Text>
Line 1
Line 2
</TextBlock.Text>
</TextBlock>
because the XML parser normalizes whitespace in element content. That CR/LF pair gets turned into a single space, and that's what gets into the text property.
If you're using binding, you don't need to worry about any of this XML stuff (unless you're binding to the contents of an XML document!). You can just put \r\n into the property value.
The other way to do this is to directly populate the TextBlock's Inlines property. But you can't do this via binding, since Inlines isn't a dependency property - in fact, it's read-only, and you can only populate it by calling its Add or AddRange methods.
I used this code to obtain what you want. This is the XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" VerticalAlignment="Center"
FontSize="16" FontFamily="Arial" FontWeight="Bold"
Text="{Binding Path=ButtonText}">
</TextBlock>
</Grid>
and this is the code behind. To make the example simplier, I don't create a ViewModel class:
public Window1()
{
InitializeComponent();
DataContext = this;
ButtonText = "Change\r\nCasette";
}
public string ButtonText
{
get { return (string)GetValue(ButtonTextProperty); }
set { SetValue(ButtonTextProperty, value); }
}
// Using a DependencyProperty as the backing store for ButtonText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonTextProperty =
DependencyProperty.Register("ButtonText", typeof(string), typeof(Window1), new UIPropertyMetadata(""));

How to set TextSearch.Text for a combobox whose TextBlock uses converter?

In the below code, Combobox is wired to NameInfo object along with a converter.NameInfoConverter returns a format in which items in combobox are shown in a particular format (for eg: LastName, FirstName (Badge#) )
Now, when I set TextSearch.Text="{Binding NameInfo, Converter={StaticResource NameInfoConverter}, ConverterParameter=true}" on combobox; TextSearch doesn't work. When I set TextSearch.TextPath="Name", search itself works but doesnot get the correct format displayed in the selectionbox of combobox.
Any Ideas?
<StackPanel>
<ComboBox x:Name:"cmbName">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name,
Converter={StaticResource NameInfoConverter}, ConverterParameter=true}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
You've probably hit a limitation in the API. I suggest you take an alternative route and bind directly to a property that is correctly formatted for your textblock.
If this is a serious app, you may want to look into using the MVVM pattern and place your converted/formatted property in the viewmodel. Otherwise, just create a new property on your databound class called NameInfo or something and do the conversion from that.

How to do simple Binding in Silverlight?

I understand that Silverlight 3.0 has binding but just want a simple example on how to use this to read a property from a class.
I have a class called Appointment which as a String property called Location:
Public Property Location() As String
Get
Return _Location
End Get
Set(ByVal Value As String)
_Location = Value
End Set
End Property
With a Private Declaration for the _Location as String of course.
I want a XAML element to bind to this property to display this in a TextElement, but it must be in XAML and not code, for example I want something like this:
<TextBlock Text="{Binding Appointment.Location}"/>
What do I need to do to get this to work?
It has to be a Silverlight 3.0 solution as some WPF features are not present such as DynamicResource which is what I'm used to using.
Just to add that my XAML is being loaded in from a seperate XAML File, this may be a factor in why the binding examples don't seem to work, as there are different XAML files the same Appointment.Location data needs to be applied.
You have two options.
If the "Appointment" class can be used as the DataContext for the control or Window, you can do:
<TextBlock Text="{Binding Location}" />
If, however, "Appointment" is a property of your current DataContext, you need a more complex path for the binding:
<TextBlock Text="{Binding Path=Appointment.Location}" />
Full details are documented in MSDN under the Binding Declarations page. If neither of these are working, make sure you have the DataContext set correctly.
You need something in code, unless you want to declare an instance of Appointment in a resource and bind to that but I doubt thats what you want.
You need to bind the Text property to the Property Path "Location" then assign the DataContext of the containing XAML to an instance of the Appointment:-
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding Location}" />
</Grid>
Then in the control's load event:-
void Page_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new Appointment() { Location = "SomePlace" };
}
Note in this case I'm using the default Page control.
If I'm reading correctly, you need to create an instance of Appointment, set the DataContext of the control to that instance and modify your binding to just say: Text="{Binding Location}"
Also, consider implementing INotifyPropertyChanged on your Appointment class to allow the data classes to notify the UI of property value changes.

Resources