I'm attempting to use a Silverlight ComboBox with a static list of elements in a very simple binding scenario. The problem is the selected item is not returning me the Text of the TextBlock within the ComboBox and is instead returning "System.Windows.Controls.TextBlock".
My XAML is:
<ComboBox SelectedValue="{Binding Country, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}">
<TextBlock FontSize="11" Text="Afghanistan" />
<TextBlock FontSize="11" Text="Albania" />
<TextBlock FontSize="11" Text="Algeria" />
</ComboBox>
In my C# file I'm binding to the ComboBox using:
Customer customer = new Customer() { Country = "Albania" };
DataContext = customer;
The binding does not result in Albania as the selected country and updating the ComboBox choice results in the Country being set to "System.Windows.Controls.TextBlock". I've tried fiddling around with DisplayMemberPath and SelectedValuePath but haven't found the answer. I suspect it's something really simple I'm missing.
Changing the ComboBox items to strings works. I'm not sure how you'd go about handling the case where TextBlocks were required for formatting...
<ComboBox SelectedValue="{Binding Country, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}">
<System:String>Afghanistan</System:String>
<System:String>Albania</System:String>
<System:String>Algeria</System:String>
</ComboBox>
Related
I have a card in Xamarin Forms that I want to be visible only if a button is visible as well. For some reason the same IsVisible attribute for the button isn't working for the card, so is there a way for me to say if the button is visible, let the card be visible?
<cor:BindableToolbarItem x:Name="Inside"
Command="{Binding InsideCommand}"
IsVisible="{Binding SelectedReading.Inside, Converter={StaticResource valueIsNotNull}}"
Icon="hi.png"
Order="Primary" />
That's the button on the toolbar:
<controls:InsideCard
BindingContext="{x:Reference Inside}"
IsVisible="{Binding SelectedReading.Inside, Converter={StaticResource valueIsNotNull}}">
</controls:InsideCard>
As you can see the IsVisible is the same for both, but it isn't working, so I tried referencing the button inside the card but it doesn't work obviously. How can I do it properly?
The BindingContext is set to the Inside element hence the SelectedReading.Inside property will not be available in the Inside object. Hence the binding won't work. Bind the IsVisible property of Inside.
<controls:InsideCard BindingContext="{x:Reference Inside}" IsVisible="{Binding IsVisible}"/>
Or set the source of binding to {x:Reference Inside} in Binding markup itself.
<controls:InsideCard IsVisible="{Binding IsVisible, Source={x:Reference Inside}}"/>
Just to add:
Moreover binding same property to two different Views should work too (If you are setting it in ViewModel). Or also setting Mode as TwoWay for Inside element. For both the cases you must have implemented the NotifyPropertyChanged for the binded property of ViewModel.
<Label
x:Name="label1"
IsVisible="{Binding ViewVisiblity}"
Text="Hi there"/>
<Label
x:Name="label2"
Text="Hi there2"
IsVisible="{Binding ViewVisiblity, Mode=TwoWay}"/>
I'm experimenting with the new SwipeView controls in Xamarin forms 4.4, and have been going through the FavFighters example (Code on Github) and think it's brilliant.
I'm able to add a Swipe right/left gesture as I wanted, but I can't find out how to add an OnClick event to an item in the list, in a Master/Detail type of project. I was hoping to use the swipe left/right actions for shortcuts, but want the user to click on the listed item to drill-into it further.
Is there a way of adding an OnClick (or an onSelected) event to the SwipeView items?
Yes you can definitely go with Xamarin.Forms.Effects which allows you implement different effects with native features.
You can refer this: https://github.com/mrxten/XamEffects it shows how to have tap effect, long tap effect, which will be helpful in your case [you can do that even without installing third party plugin].
Do you want to achieve the result like following GIF.
If you want to add the click event for your SwipeView. you just add the GestureRecognizers in your swipeView like following code.
<SwipeView>
<SwipeView.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</SwipeView.GestureRecognizers>
<SwipeView.LeftItems>
<SwipeItems>
<SwipeItem Text="Favorite"
BackgroundColor="LightGreen"
Invoked="SwipeItem_Invoked" />
<SwipeItem Text="Delete"
BackgroundColor="LightPink"
Invoked="SwipeItem_Invoked_1" />
</SwipeItems>
</SwipeView.LeftItems>
<!-- Content -->
<Grid HeightRequest="60"
WidthRequest="300"
BackgroundColor="LightGray">
<Label Text="Swipe right"
HorizontalOptions="Center"
VerticalOptions="Center" />
</Grid>
</SwipeView>
In the background code.
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
Navigation.PushModalAsync(new Page1());
}
Here is the XAML i use to display the Listbox:
<ListBox x:Name="groupedList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate x:Name="myTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Grid.Row="0" FontWeight="Bold" x:Name="templateHeader" />
<TextBlock Grid.Row="1" x:Name="templateCaption"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The problem is that the Databinding has to be created in code since there is different data each time. (In fact the data comes by using an indexer[] with a dynamically selected field in a sql). My question now is how to apply the Databinding to the Listbox and the DataTemplate from code. As far as i can see there is no way access the items defined inside of a DataTemplate.... Can anyone help ?
This is my attempt so far:
Binding binding = new Binding("["+CaptionField+"]");
binding.Source = this.controller.table.Rows;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this.groupedList.Items, ItemsControl.ItemsSourceProperty, binding);
this.groupedList.SetBinding(null, binding);
I was about to give you a simple example but I think there are plenty of better example, so please take a look at these site to see if it help:
WP7 - listbox Binding
listbox-data-binding
Please comment if anything wasn't clear.
In SL4 DataGrid I have the following multicontrol column:
<sdk:DataGridTemplateColumn Header="Address Line1
Address Line 2" MinWidth="200">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Address1}"/>
<TextBlock Text="{Binding Path=Address2}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<TextBox Background="Transparent" BorderThickness="0"
TabIndex="0"
Text="{Binding Path=Address1, Mode=TwoWay}"/>
<TextBox Background="Transparent" BorderThickness="0"
TabIndex="1"
Text="{Binding Path=Address2, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
In edit mode pressing Tab key while at address1 moves focus to next DataGrid column but not to address2 textbox. If I delete CellTemplate and CellEditingTemplate to be CellTemplate instead, then TabIndex works as expected, however, current column stay the same, so if datagrid has many columns, some of which are hidden, then auto scrolling doesn't occur. What should I do to solve this problem?
A little bit late, but i found a workaround for this problem.
Just add a KeyDown EventHandler to your CustomControl:
private void address1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Tab) && address2.IsEnabled)
{
address2.Focus();
e.Handled = true;
}
}
Probably, having multiple controls inside datagrid cell is bad idea. If multiple controls needs to be inside cell, then better way seems to be to create custom composite control and place it inside cell.
I am using a code snippet from codeproject of dropdown button.
< m:SplitButton Content="TWB" Name="btnSearch"
Grid.Row="0" Grid.Column="0"
Style="{StaticResource aeroNormalColorSplitButtonStyle}"
Click="btnSearch_Click"
Width="60" Height="30"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Mode="{Binding ElementName=modeSelector, Path=SelectedItem}"
Placement="{Binding ElementName=placementSelector, Path=SelectedItem}" MouseLeftButtonDown="btnSearch_MouseLeftButtonDown"
MenuItem Header="TWB"/>
MenuItem Header="PWB">
/MenuItem>
</m:SplitButton>
so, beside this splitdown button, I have a textbox(which is basically a search box). So, as the above code snippet shows the 2 menu items as "TWB" and "PWB", I have to fill the textbox when TWB is selected from the dropdown button and also the same text should be displayed on the button too (TWB).
if i click PWB from the dropdownbutton, I should get the "PWB" name on the button and the same PWB name should be displayed in the textbox too.
Please help me.
Thank You,
Ramm
I tried by adding ListBox to the SplitButton, Now I am able to see the text in the textbox.
Modified the above Xaml code to
<m:SplitButton Content="TWB" Name="btnSearch"
Grid.Row="0" Grid.Column="0"
Style="{StaticResource aeroNormalColorSplitButtonStyle}"
Click="btnSearch_Click"
Width="60" Height="30"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Mode="{Binding ElementName=modeSelector, Path=SelectedItem}"
Placement="{Binding ElementName=placementSelector, Path=SelectedItem}">
<ListBox x:Name="TransitionKind" SelectionChanged="TransitionKind_SelectionChanged">
<ListBoxItem Content="TWB"/>
<ListBoxItem Content="PWB"/>
</ListBox>
</m:SplitButton>
in the code behind,
btnSearch.Click += new RoutedEventHandler(btnSearch_Click);
TransitionKind.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(TransitionKind_SelectionChanged);
private void TransitionKind_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
btnSearch.Content = ((ListBoxItem)TransitionKind.SelectedItem).Content;
txtBxSearch.Text = "Search " + (string) btnSearch.Content;
}
Now, I am able to see the text in the textbox changing to the click of dropdown button.
Thank you,
Ramm