Localize text that is already data binding? WP8 - c#

I have a Longlistselector that is getting the text through data binding. My problem is I need it to be localized so it displays the right language, how is the right way to do it?
I tried the first way that came to my head, but I thought it doesn't work:
MainPage.cs
_UserAdBL.Add(new UserAdB("{Binding LocalizedResources.UsText01, Source={StaticResource LocalizedStrings}}"));
Any help will be appreciated, thanks!

If I understand you correctly, you want to get the value of the {Binding LocalizedResources.UsText01, Source={StaticResource LocalizedStrings}} XAML binding in C#.
So use AppResources.UsText01 like so
_UserAdBL.Add(new UserAdB(AppResources.UsText01));

Related

WPF How to use GridView to display a matrix

I am new to WPF programming and I am trying to convert my python program into a UWP app just for practice. The app can dynamically creates new text entries and labels to allow users to input the entries of matrices.
I am currently stuck in how to create a grid to put those text entries and labels. I thought a right choice would be using gridView but the examples that I have seen don't seem to help. Any ideas on how to implement that?
This is my current try:
<GridView x:Name="gridView" Margin="0,150,0,0">
<GridView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
I am also trying to access the Columns attribute of the gridView (System.Windows.Controls) but it seems I am using the wrong gridView (Windows.UI.Xaml.Controls). But VS can't find the correct gridView for me as it doesn't exist. I am very confused with GridView. Can anyone provide some pointers?
ok, so I think your question is : WPF - How to bind a GridView to a Matrix or How to display a Matrix in WPF, if I understood good what you want, this link seems a good solution to your problem on how to bind your Matrix to DataGrid.
For that you need to :
create two ObservableCollection< int >(or ObservableCollection< string > lists in your ViewModel
(For example ListRows and ListColumns)
When you edit your textboxes, you will update ListRows or ListColumns by updating the corresponding ObservableCollection
So using binding, the GridView will automaically update.
But before starting with it, I you need some notions with Binding, ViewModel, ObservableCollection and Converters

Windows Phone 8 Font Binding

I'm really stumped on how to go about binding fonts to a textbox. I'd like for my user to be able to choose between 3-4 different fonts. I have something like this right now:
<TextBox x:Name="MyTextBox" AcceptsReturn="True" FontSize="20"
FontFamily="{Binding FontSelection}" />
But I have no clue how the c# should look, and I had no luck googling for it. What is the best way to do this? Do I have to create an observable collection? I've tried adding fonts to the Application.Resources, but it wouldn't let me.
Thanks for any answers!
This FontSelection property has to be single item. You could first try simple valid string (like 'Arial'), it could work. Another approach is FontFamily type as poined out by Romasz.
Also there are Converters available, quite easy to implement. http://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-25-Advanced-Binding-with-Value-Converters With this you could save user font selection as simple number for example and convert it to appropriate type with Converter.

WPF Datagrid Combobox XAML and Codebehind

Since I am a beginner in WPF, I have a question which might be basic in nature.
I have a datagrid which has a combobox.
The xaml which I have written is:
<DataGridComboBoxColumn Header="ControlOption" Width="100"
SelectedItemBinding="{Binding Path=DataGridComboxBox_Control}">
<DataGridComboBoxColumn.ItemsSource>
<col:ArrayList>
<sys:String>Database1</sys:String>
<sys:String>DataBase2</sys:String>
</col:ArrayList>
</DataGridComboBoxColumn.ItemsSource>
</DataGridComboBoxColumn>
I have 3 values which should be there in the drop down of the comboBox.
1. WorkStream1
2. WorkStream2
3. WorkStream3
Now how do I write the codebehind for the ComboxBox.
Please do give me pointers in this. :)
Very much appreciated.
Ashutosh
Sorry, haven't quite understood you. Do you want to replace the itemssource of combobox from code behind?
In this case you can to bind combobox's ItemsSource to property. Like that:
<Controls:DataGridComboBoxColumn Header="Gender" ItemsSource="{Binding Path=Genders}" />
and in code behind will be smth like
public ObservableCollection<string> Genders
{
get {
return _genders;
}
set { _genders = value;
PropertyChanged(this, new PropertyChangedEventArgs("Genders"));
}
}
If you meant smth else, explain please. May be I've understood you in a wrong way
use datagrid_PreviewKeyDown function to capture keypressed in grid. if the cell is focused use
datagrid.BeginEdit() to convert cell from text block to combo box.
i think this will help to solve the issue.

Two-way binding an ObservableCollection<string> to WPF DataGrid

I've been tying different things / reading up on this issue for awhile now and have not yet found an answer. Hopefully you guys can help.
I have an observablecollection of type string. I want to bind this collection to a datagrid and be able to edit/delete/add to the collection. Here is my xaml:
<DataGrid ItemsSource="{Binding Movies.Titles}" CanUserDeleteRows="True" CanUserAddRows="True" Height="300">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
</DataGrid.Columns>
</DataGrid>
The same observablecollection is also bound to a listbox. I want to be able to edit the collection using the datagrid method (above) and see the changes/edits in the listbox. The delete/add is working correctly, but when I edit a string inside a grid cell and it loses focus, the string goes back to what it originally was and never gets updated.
Thanks a lot for any help / suggestions.
Wow, I went to do this yesterday and was stuck with a DataGrid that would add a new line for my ObservableCollection. After research, I realized why. Strings and immutable.
I found this question and unfortunately it didn't have an answer. So I can't leave this empty of an answer.
So here are the answers I found:
DataGrid cannot update a string collection by adding, editing, or removing strings.
I found a workaround to wrap the string in a StringWrapper object. Here it is.
public class StringWrapper
{
public string Text { get; set; }
}
I didn't like either answer.
The original question asker, moncadad, looks like he wants a one column DataGrid. He probably just wants to add and remove strings from an ObservableCollection without a lot of code. Edit probably isn't too important since that can be done by delete and add again.
I ended up doing this myself with a reusable usercontrol I made called StringListBox.
A ListBox for strings that supports add and delete
Basically the idea is to create the look of a DataGrid with a Label, a ListBox, a TextBox and an Add button and since this is a control, it has to work with ObservableObject or List in one control.
This give you Add and Delete. I doesn't provide edit.
Hope this helps the next guy.
Actually it works, you should just use
Mode=OneWay
in your binding.
I hope this helps!

Silverlight, how to make List of a UserControl (In ContentControl?)

i've been banging my head on this for the last hours...
I have a User Control called "DayItem", and i want to show it 48 times in another UserControl called "DayPanel".
Let me mention this is done in MVVM style, but i'm only experiencing, and a straight way would by fine for an answer.
I have an ObservableCollection<DayItem> in the DayPanel model, and in the Xaml there's an <ItemsPresenter />.
if i do
this.ItemsSource = DayItems;
everything show up fine.
but, i wanna be able to use those DayItems in the UI like a list... to support multi-select etc.
so i tried using a ContentControl, and set it's content to the ObservableCollection.
but it just shows the ObservableCollection object's ToString text.
so i guess i need a DataTemplete there...
but why do i need a DataTemple to show a Control?
it's already styled in it's own Xaml, i don't wanna repeat it's styling again.
or maybe i'm totally wrong, anyway i need help :x
Edit:
I got this to work, saying what DataType wasn't necessary or even possible.
and in the code behind i told the listbox, that it's ItemSource was the ObservableCollection.
now i've ran into other problems... ListBox related...
There are Gaps between each control in the ListBox, which messes up the layout
and also i need to figure out a way to select multiple items by dragging...
thanks for the help so fat
First, you need a view model for you DayItem user control. Lets call it DayItemViewModel. Also I suppose you DayPanel also has a view model called something like DayPanelViewModel. Then, you DayPanelViewModel would expose a collection of DayItemViewModel instances:
public class DayPanelViewModel
{
public ObservableCollection<DayItemViewModel> DayItems { get; set; }
}
Then, in your DayPanel.xaml:
<UserControl x:Class="DayPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<DataTemplate x:Key="DayItemTemplate"
DataType="{x:Type my:DayItemViewModel}">
<my:DayItem />
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding DayItems}"
ItemTemplate="{StaticResource DayItemTemplate}" />
</Grid>
</UserControl>
Try using ListBox, since that implements multiselect...
Also it might be wise (for MVVM) if you do not contain DayItems, but DayItemModel's in your DayPanelModel, and set the ListBox's ItemTemplate to present each DayItemModel with a DayItem.

Categories