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

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" />

Related

Is it possible to hide the value of Textbox using Password Char?

I have a XAML Code which compose of a single textbox and I want the value to be hidden like in Winforms Password Char Properties. So my question is. Is it possible to hide the value of textbox using Password Char in WPF? Thanks
<TextBox x:Name="txtUsername" Text="{Binding Path = UserProp.Username}" FontSize="14" Margin="0 10"
materialDesign:HintAssist.Hint="Username" materialDesign:HintAssist.IsFloating="True"
BorderBrush="{Binding Path=BorderColor}" />

Context menu paste in textbox not updated binding element value

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}"

Bind method result to a Silverlight XAML TextBlock

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.

json to new listbox item in windows store app C#

I have an app for windows 8 that needs to take a Json string and deseriaizes it into DATACONTRACTS and it will display the information I wish in a Listbox that will have a max height and will scroll if greater than the max height.
The problem that im having it not so much as not being able to do it but rather not knowing how to do it.
So far I can deserialize the Json and I can specify where I want each item to go into the UI but what im trying to do is basically a for each item in the array I want it to make a new Stackpanel formatted with Textblocks that will have the information from the Json. I don't know how to this unfortunately and I don't really know what im searching for to get tutorials on how to do it
This is the code I have that takes the items from the json with a helper class and puts them in the Text of the TextBlocks.
var _FilterSaleList = new FilterSalesList();
var _Sales = await _FilterSaleList.FindSalesbyFilters();
string _SaleName = _Sales.sales[0].name.ToString();
string _SaleDescription = _Sales.sales[0].description.ToString();
string _SaleName1 = _Sales.sales[1].name.ToString();
string _SaleDescription1 = _Sales.sales[1].description.ToString();
int _TotalResults = _Sales.sales.Length;
SaleTitle.Text = _SaleName;
SaleDescription.Text = _SaleDescription;
SaleTitle1.Text = _SaleName1;
SaleDescription1.Text = _SaleDescription1;
This is the XAML code for the Listbox with 2 Stack panels already in it.
<ListBox Grid.Row="1">
<StackPanel Margin="0,0,0,5">
<TextBlock x:Name="SaleTitle" Text="" HorizontalAlignment="Center" Margin="0,0,0,5"/>
<TextBlock x:Name="SaleDescription" Text="" HorizontalAlignment="Center" MaxHeight="40" Margin="0,0,0,5" TextWrapping="Wrap"/>
</StackPanel>
<StackPanel Margin="0,0,0,5">
<TextBlock x:Name="SaleTitle1" Text="" HorizontalAlignment="Center" Margin="0,0,0,5"/>
<TextBlock x:Name="SaleDescription1" Text="" HorizontalAlignment="Center" MaxHeight="40" Margin="0,0,0,5" TextWrapping="Wrap"/>
</StackPanel>
</ListBox>
Below is an image of how I would like it to look.
even though everything works this way like I said I would like it so that each item from the json will make a new stackpanel and display the information as in the image. I don't know what its called when this is done so even a simple hint as to where to look would be great!
http://puu.sh/2biMZ
In XAML there is a very nice feature called Binding, which allows you to simply bind an object or a list of objects to visual element. This way, you don't have to "build" the graphical user interface manually in C# code.
This is a very large topic, so you should probably have a look at what is MVVM, it will help you leverage the power of Binding : http://channel9.msdn.com/Series/Building-Apps-for-Both-Windows-8-and-Windows-Phone-8-Jump-Start/Building-Apps-for-Both-Windows-8-and-Windows-Phone-8-03-Model-View-ViewModel
But for now, what you could is :
1/ Define your ListBox as following, with a DataTemplate for the ItemTemplate property :
<ListBox Grid.Row="1" x:Name="SalesListbox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,5">
<TextBlock x:Name="SaleTitle" Text="{Binding name}" HorizontalAlignment="Center" Margin="0,0,0,5"/>
<TextBlock x:Name="SaleDescription" Text="{Binding description}" HorizontalAlignment="Center" MaxHeight="40" Margin="0,0,0,5" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The DataTemplate will tell how each item of the list should be rendered. You should also notice how we used Binding for the Text properties in each textblock. It's bound to name and description which are the name of the properties in your model.
And then you can populate your ListBox with your data :
var filterSaleList = new FilterSalesList();
var salesByFilters = await filterSaleList.FindSalesbyFilters();
SalesListbox.ItemsSource = salesByFilters.sales;

Issue in displaying "-" symbol in calculator application in C# WPF

Hi i am trying to learn windows presentation foundation WPF and was trying to develop the simplest application Calculator. But i am having issue in displaying the '-' when i substract a higher value from smaller. For eg. if i do something like this "10 - 20" the output should be "-10" in the screen i.e. textbox. But it is displaying "10-". Somehow the '-' is coming in the end. my xaml code for textbox looks like following:
<TextBox Height="33" HorizontalAlignment="Left" Name="outputbox"
VerticalAlignment="Top" Width="278"
FontFamily="Tahoma" FontSize="18"
FlowDirection="righttoleft" IsReadOnly="True" />
and the code for doing substraction and display looks something like this
if (entry1 > entry2)
{
outputbox.Text = (entry1 - entry2).ToString();
}
else
{
outputbox.Text = "-" + (entry2 - entry1).ToString();
}
while debugging it shows the proper string as "-10" but while displaying in the textbox it is showing the string "10-". Any idea about what is missing???
Just remove the FlowDirection attribute from your TextBox and your result will be fine.
<TextBox Height="33"
HorizontalAlignment="Left"
Name="outputbox" VerticalAlignment="Top"
Width="278"
FontFamily="Tahoma"
FontSize="18" IsReadOnly="True" />
Or you may specify FlowDirection="LeftToRight" which is the default for the TextBox
Thanks All. I removed the "FlowDirection" property and it displayed the proper "-10" but the text was now displayed on the left side of textbox. So i used the "TextAlignment = right" property and it seems to be working fine. Thanks again all of you.

Categories