I have a line chart, what I want to do is bind the data from my classList and display it on my line chart. How do i do that? my class contains a string and a double
List<myClass> classList = new List<myClass>();
classList.Add(class);
<charting:Chart x:Name="MyLineSeriesChart">
<charting:LineSeries Name="LineChart" ItemsSource="{Binding}" IndependentValuePath="X" DependentValuePath="Y" />
</charting:Chart>
Related
Here is the XAML Code from the documentation:
<StackLayout>
<CarouselView ItemsSource="{Binding Monkeys}"
IndicatorView="indicatorView">
<CarouselView.ItemTemplate>
<!-- DataTemplate that defines item appearance -->
</CarouselView.ItemTemplate>
</CarouselView>
<IndicatorView x:Name="indicatorView"/>
</StackLayout>
I am using C# Markup so my code looks like this (styling code and some layout settings removed to keep simple for SO question):
CarouselView ScrollingImageView() => new CarouselView
{
ItemTemplate = new DataTemplate(() =>
{})
}
.Bind(ItemsView.ItemsSourceProperty, nameof(_vm.ListOfItems))
.Bind(CarouselView.PositionProperty, nameof(_vm.ScrollingImageViewerPosition));
IndicatorView IndicatorView() => new IndicatorView
{
IndicatorsShape = IndicatorShape.Circle,
};
Grid ButtonIndicatorGrid() => new Grid
{
Children = { IndicatorView().Row(0).Column(1) }
};
void Build() => Content =
new Grid
{
Children = {
ScrollingImageView().Row(0),
ButtonIndicatorGrid().Row(1),
}
};
In this example, the IndicatorView is rendered beneath the
CarouselView, with an indicator for each item in the CarouselView. The
IndicatorView is populated with data by setting the
CarouselView.IndicatorView property to the IndicatorView object. Each
indicator is a light gray circle, while the indicator that represents
the current item in the CarouselView is dark gray. Setting the
CarouselView.IndicatorView property results in the
IndicatorView.Position property binding to the CarouselView.Position
property, and the IndicatorView.ItemsSource property binding to the
CarouselView.ItemsSource property.
Now I cannot find an example of how to do this with C#. In XAML it works by setting the name but how can I do the same thing in C# as from what I understand (please correct me if wrong), I cannot set the name in C# in the same way as in XAML.
you need to maintain a reference to your UI objects when you create them
var iv = IndicatorView();
var cv = ScrollingImageView();
cv.IndicatorView = iv;
In my Xamarin.Forms Shell application, I define the flyout items in the AppShell.xaml.cs file and not in the AppShell.xaml one since I need to define them programmatically. Here, I read I can use the FlyoutDisplayOptions.AsMultipleItems value to get separators. So, I don’t understand why Xamarin doesn’t show separators when I use the ShellSection elements and set their FlyoutDisplayOptions as FlyoutDisplayOptions.AsMultipleItems. The code through which I define the flyout items is the following:
var nli = new FlyoutItem { FlyoutDisplayOptions = FlyoutDisplayOptions.AsMultipleItems };
var nc = new ShellSection { FlyoutDisplayOptions = FlyoutDisplayOptions.AsMultipleItems };
foreach (var kind in kinds) { // "kinds" is retrieved from a service
var csc = new ShellContent { /* ... */ };
nc.Items.Add(csc);
}
nli.Items.Add(nc);
ShellItems.Items.Add(nli);
The red area is the one populated by the foreach statement. The FlyoutItem and ShellSection parents have the FlyoutDisplayOptions correctly set, but the separators are not shown between the ShellContent elements. Why?
To add a separator between FlyoutItems or MenuItems, you can add a MenuItem with a DataTemplate as below:
<MenuItem>
<Shell.MenuItemTemplate>
<DataTemplate>
<Label HeightRequest="1" BackgroundColor="LightGray"></Label>
</DataTemplate>
</Shell.MenuItemTemplate>
</MenuItem>
You can perhaps convert this into code-behind.
Here is how it looks:
It should be a default phenomenon in shell, there is no separators between the items in FlyoutItem.
You could have a look at the official document:
I want to display a binding field in a label. It's easy in XAML but how to do it in code-behind? In XAML,
<Label Text="{Binding LastName}" Style="{StaticResource MyLabel}"/>.
In my code behind, I have tried:
Label ln = new Label();
ln.BindingContext = "ContactsModel";
ln.SetBinding = "LastName";
which does not work and I have no clue how to set the Style.
If you read the Basic Binding documentation, then it clearly states that to bind a view you have to.
Specify a BindingContext
Use the SetBinding method to specify the target property to be bound to which ViewModel source property.
The BindingContext may be inferred from the parent element and does not always have to be specified, but your binding should look more like:
var label = new Label();
label.SetBinding(Label.TextProperty, "LastName");
This will bind the Text property on the label to LastName in the ViewModel.
I have a implement syncfusion Carousel and binding items using ItemTemplate.When i load the items to Carousel all item appears in Carousel view.But i need to add a doted indicator for it.
When user swipe though the Carousel by the dots should indicate current position.
When reading from documentation of syncfusion rotator have this functionality.
I need to add this to carousel view.
Here you can find all the SfCarousel Class Members.
And there's no property for the dots you refered in the SfCarousel print.
In fact, I think you are confusing it with another component called SfRotator. (that has an identical example like your print). and the property you are looking for is called: DotPlacement.
And can have the following states:
None //No Dots
Default //Dots Inside the Rotator View
OutSide //Dots Outside the Rotator View
We have analyzed your query and currently we don’t have dots view support in CarouselView. However, we can fulfill this requirement by using Border or Button control as like below code snippet.
Custom DotsView:
XAML:
<border:SfBorder BorderColor="{Binding ThumbBorder}" HorizontalOptions="Center" VerticalOptions="Center" BorderWidth="5" CornerRadius="50" />
Carousel View:
XAML:
<carousel:SfCarousel x:Name="carousel" Grid.Row="0" Offset="0" ItemsSource="{Binding ImageCollection}" ItemTemplate="{StaticResource itemTemplate}"
ItemHeight="200" SelectionChanged="Carousel_SelectionChanged"
ItemWidth="200" />
<StackLayout Grid.Row="1" HorizontalOptions="Center" x:Name="rotatorThumb" BackgroundColor="Transparent" Orientation="Horizontal"/>
C#:
public MainPage()
{
InitializeComponent();
Command command = new Command((object thumb) =>
{
var thumbView = thumb as DotsView;
if (thumbView != null)
{
((rotatorThumb.Children[carousel.SelectedIndex] as DotsView).BindingContext as CarouselModel).
ThumbBorder = Color.LightGray;
carousel.SelectedIndex = thumbView.Index;
(thumbView.BindingContext as CarouselModel).ThumbBorder = Color.Red;
}
});
for (var i = 0; i < ImageCollection.Count; i++)
{
var itemView = new DotsView() { BindingContext = ImageCollection[i], Index = i };
if (carousel.SelectedIndex == i)
(itemView.BindingContext as CarouselModel).ThumbBorder = Color.Red;
TapGestureRecognizer thumbTap = new TapGestureRecognizer();
thumbTap.Command = command;
itemView.GestureRecognizers.Add(thumbTap);
thumbTap.CommandParameter = itemView;
rotatorThumb.Children.Add(itemView);
}
}
Output:
Sample
I'm facing an issue in Xamarin forms Mvvm. I have 2 different layouts say Layout1 and Layout2 which are bounded with a common ViewModel. Layout1 contains multiple Labels which I'm generating dynamically using for loop in xaml.cs file and bind each Label'sTextProperty using SetBinding. Layout2 contain a button.
Now I want to change Text of a particular Label when button clicked.
Layout1.xaml
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Layout1">
<StackLayout x:Name="ParentStack">
// dynamic Labels to be added here..
</StackLayout>
</StackLayout>
Layout1.xaml.cs
public partial class Layout1: StackLayout
{
public Label dummyLabel;
public Layout1()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
dummyLabel= new Label
{
Text = " ",
};
dummyLabel.SetBinding (Label.TextProperty,"PhaseValue");
parentRowCells.Children.Add(dummyLabel);
var tapGestureRecognizer_1 = new TapGestureRecognizer();
tapGestureRecognizer_1.SetBinding(TapGestureRecognizer.CommandProperty,"LabelClicked");
tapGestureRecognizer_1.CommandParameter = dummyLabel;
dummyLabel.GestureRecognizers.Add(tapGestureRecognizer_1);
}
}
}
Layout2.Xaml
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Layout2">
<StackLayout x:Name="ParentStack">
<Button Command={Binding ButtonClickedCommand} Text="Click Me" />
</StackLayout>
</StackLayout>
ViewModel.cs
class ViewModel
{
public Label label = new Label();
public string textstring = "new text string";
ICommand _labelClicked;
public ICommand LabelClicked
{
get
{
this._labelClicked= this._labelClicked?? new Command(s =>
{
label = s as Label;
label.Text = "new text"; //this change the text of particular label when clicked but i need it from button clicked event from another layout.
// here I'm getting the instance of label which i clicked on label.
});
return this._labelClicked;
}
}
public ICommand ButtonClickedCommand{ protected set; get; }
public ViewModel()
{
this.ButtonClickCommand = new Command<Button>((key) =>
{
//here I want to change the value of label when button command is clicked.
aa.Text = "this is not changing the text";
});
}
}
Any help in this or do I need to follow some other pattern..??
My first thought would be to add each Label that you add to a List<Label> somewhere that you can access from both layouts... your View Model would seem like the logical place. Then when you click your button, you can find the particular Label whose text you want to change and change it. You will likely then have to reload your list.
However I think that a better way would be to use a ListView instead of a StackLayout. Then you can have an ItemTemplate for the ListView that includes one Label. You can then set up an ObservableCollection<T> of objects to use as the ListView.ItemsSource. You would want to make some custom object that has a Text property, or whatever you want to call the property that will hold the text for the Labels. It is better to use an object for the T in ObservableCollection<T> rather than using ObservableCollection<string> because changes to a string type will not be reflected in the ListView item, but changes to a property of an object (assuming of course that you make it a Bindable Property) will be reflected in those controls that are bound to that property. So in a nutshell, something like (in your ViewModel):
// Class level variable
ObservableCollection<CustomType> dummyLabelContents;
// then either in the ViewModel constructor or somewhere else:
dummyLabelContents = new ObservableCollection<CustomType>();
CustomType dummyText;
for (int i = 0; i < 3; i++)
{
dummyText = new CustomType
{
Text = " ",
};
}
dummyLabelContents.Add(dummyText);
And your CustomType would just be a simple class with only a BindableProperty called Text.
Set up like this, you can assign your ListView.ItemsSource to be the dummyLabelContents ObservableCollection and then whenever you add an item to the ObservableCollection, the ListView will update automatically. Also, since using a custom type with a bindable text property in the ObservableCollection, when that text property is changed the item in the ListView should also update accordingly.