I have a function which, given a string, returns the string translated to German, linked to the xaml I want to edit:
public string convert (string label) {
return Translator.translate (label);
}
This string is then used to label some parts of my interface with TextBlocks in Silverlight. This is one sample TextBlock of my code:
<TextBlock Text="Center" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" />
I would wish to modify the "Text" property to call convert passing the text "Center" as a parameter and show the return result as the Text. Is there any way to do that?
Greetings
You can make it a converter and apply it to the binding base in your TextBlock kind of like;
<TextBlock Text="{Binding Text,
Converter={StaticResource YourTranslatorConverterThingy}}"/>
Here's just one article with a bit more explanation. Hope this helps.
Related
how to write a custom setter for a dependency object in wpf using mvvm ?
In my ViewModel I have a dependency object called Seasonalprop which I use to bind to a TextBox in XAML. I would like to write a custom setter, so that it notifies the user when the provided string input cannot be converted to double.
The error that I am getting is that value is a string and cannot be converted to double.
public double Seasonalprop
{
get { return (double)GetValue(SeasonalProperty); }
set
{
try
{
Double.TryParse(value, out parsedouble);
SetValue(SeasonalProperty, value);
}
catch(Exception ex)
{
MessageBox.Show(" String Input cannot be converted to
type double");
}
}
}
I think you want to write a custom settor so that it notifies the user if the value in text box is invalid.
Have a look at the docs for validation in WPF
The text content of the TextBox in the following example is bound to
the Age property (of type int) of a binding source object named ods.
The binding is set up to use a validation rule named AgeRangeRule so
that if the user enters non-numeric characters or a value that is
smaller than 21 or greater than 130, a red exclamation mark appears
next to the text box and a tool tip with the error message appears
when the user moves the mouse over the text box.
I think your whole concept was gone to a wrong direction. First of all, when a binding expression updates a dependency property, it will call its onwer's SetValue method but not its clr property wrapper. So a custom setter would do nothing for you in this situation. And as #peeyushsingh's answer, wpf has binding validation for this. So, something you need should be like:
<TextBox Text="{Binding Seasonalprop, ValidatesOnExceptions=True}">
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<Border BorderThickness="1" BorderBrush="Red" >
<AdornedElementPlaceholder/>
</Border>
<TextBlock Foreground="Red" Margin="2" Name="cc"
Text="! Not a double."/>
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
<TextBox x:Name="V2" Grid.Row="2" Grid.Column="8" Grid.ColumnSpan="3" Text="{Binding airPlane}".../>
In the above code, if I want to add more things in the "Text" property besides binding the airplane data, e.g. to add a unit called "Pounds", how to modify the code? Thanks!
You can use Converters for it. For more see WPF Converters
I'm using the XCeed Extended Toolkit Plus. The charting specifically. However, I don't think question need to be specifically about this control, more about how to pass or share values between controls and templates.
The charts are in a WPF UserControl, but the values are set in the code behind. There is no data binding or MVVM, more of a 'winforms' approach.
The charting works, but I want to alter the way the bar's (in a bar graph) display.
According to the documentation, I can use a Template
<xctk:Chart >
<xctk:Chart.Areas>
<xctk:Area x:Name="MyGraphArea">
<xctk:Area.XAxis>
<xctk:Axis Title="Date"
/>
</xctk:Area.XAxis>
<xctk:Area.YAxis>
<xctk:Axis Title="Position"
/>
</xctk:Area.YAxis>
<xctk:Area.Series>
<xctk:Series Template="{StaticResource SeriesTemplate}" >
<!--done in code behind-->
</xctk:Series>
</xctk:Area.Series>
</xctk:Area>
</xctk:Chart.Areas>
</xctk:Chart>
On the same XAML page, I also have in my Grid.Resources. This is where the problem is
<DataTemplate x:Key="SeriesTemplate">
<Button x:Name="Bar">
<StackPanel>
<DockPanel>
<TextBlock x:Name="seriesTemplateDate" Text="How To I Bind"></TextBlock>
</DockPanel>
<DockPanel>
<TextBlock x:Name="seriesTemplatePosition" Text="What Am I binding too"></TextBlock>
</DockPanel>
</StackPanel>
</Button>
</DataTemplate>
And in my code behind (showing constructor)
public GraphView(IEnumerable<DataPoint> graphData, string title)
{
InitializeComponent();
var series = new Series();
foreach (var dataPoint in graphData)
{
series.DataPoints.Add(dataPoint);
}
series.Title = title;
this.MyGraphArea.Series.Add(series);
}
So, the DataTemplate is where I'm having an issue. I have no idea what to enter the for text value
I don't think I can add the value by name and set it in the code behind because it gets called on each iteration (depending on how many items are in the series).
The only way, in my head, is if it the DataTemplate can inherit some how from the calling control. At this stage, my Google results were providing nothing useful, and I think I'm getting myself muddled!
I have to guess since I do not know the XCeed Extended Toolkit Plus. That being said, the Datacontext of a Datatemplate is usually set to the data object. In this case that should be the series object. This means you can bind directly to properties on the series object like the Title-property. Maybe it also has a Date and Position property?!
series.Title = title;
series.Date = date;
series.Position = position;
<TextBlock x:Name="seriesTemplateTitle" Text="{Binding Title}"></TextBlock>
<TextBlock x:Name="seriesTemplateDate" Text="{Binding Date}"></TextBlock>
<TextBlock x:Name="seriesTemplatePosition" Text="{Binding Position}"></TextBlock>
Good Luck!
At the very simplest level all I am trying to do is Data Bind TextBlock control (XAML). Am tying to get string from MyString (property defined in C# code behind) as a Text for TextBlock:
DisplayText disp = new DisplayText();
disp.MyString = "Hello";
public class DisplayText {
public string MyString {get;set;}
}
XAML code:
<TextBlock Grid.Column="1" Text="{Binding Path=MyString}" Foreground="Black"/>
But, Its not working:( Am searching for hours but could'nt get this simple thing done. Plz help!
In your XAML you need to define the DataContext.
For example:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Also, you'll need to implement INotifyChanged if you want the screen and model to stay in sync.
Have you seen the msdn article about data binding in Store Apps? http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464965.aspx
The example code shows how to do what you are describing and worked for me.
I'm doing a little app that basically has 2 text boxes. You'll enter Fahrenheit into TextBoxA and Celsius into TextBoxB.
As the text changes in TextBoxA I want the equivalent Celsius value to be displayed in TextBoxB and vice versa.
I can come up with a solution pretty easy for this but i'm trying to be a little clever.
Is there a way to do it all in Xaml except for a Convert class that does the maths? So basically I want the TextChanged event of one textBox to pass in it's value into a Converter class that is evaluated and sent to the other TextBox and visa versa.
Anyone know how I can achieve this ... and if it's possible at all?
<TextBox x:Name="Celsius" />
<Textbox x:Name="Fahrenheit" Text="{Binding Text, ElementName=Celsius, Mode=TwoWay, Converter={StaticResource CelsiusToFahrenheitConverter}}" />
When you update C, F will get the value and convert it. When you update F, it will convert back to C and push the value to the Celcius textbox.
<TextBox x:Name="CelcuiusTextBox" Text="{Binding Text, ElementName=FahrenheitTextBox, Converter={StaticResource FahrenheitToCelciusConverter}}"/>
<TextBox x:Name="FahrenheitTextBox" Text="{Binding Text, ElementName=CelciusTextBox, Converter={StaticResource CelciusToFahrenheitConverter}}"/>
Assumes the existence of two distinct converters. Of course, you could write a single converter that can be put in a different mode to convert either from or to celcius.