I have a win phone chart control and want to show data based on three condition. So I am using a bar series and each bar satisfy any one of the three condition. So I just want to show bars in three different colors each for a condition.
<chart:ColumnSeries Label="Series1" ItemsSource="{Binding CategoricalDatas}"
XBindingPath="Category" YBindingPath="Value" Palette="Custom"> <chart:ColumnSeries.ColorModel>
<chart:ChartColorModel>
<chart:ChartColorModel.CustomBrushes>
<SolidColorBrush Color="#F02B2C"/>
<SolidColorBrush Color="#FF3B5C"/>
<SolidColorBrush Color="#F06B3C"/>
<SolidColorBrush Color="#FFFB6C"/>
<SolidColorBrush Color="#F0FB8C"/>
</chart:ChartColorModel.CustomBrushes>
</chart:ChartColorModel>
</chart:ColumnSeries.ColorModel>
This is a simple way to put different colors for bars. How I specify condition?
You can add the condition in Chart Series ColorModel converter and you can return custom brush based on specified condition as like the below code snippet.
Code Snippet[C#]:
viewmodel model1 = new viewmodel();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
model1.CustomBrush = new List<Brush>();
foreach (var item in model1.Products)
{
if (item.mark >= 80)
{
model1.CustomBrush.Add(new SolidColorBrush(Colors.Green));
}
else if (item.mark < 50)
{
model1.CustomBrush.Add(new SolidColorBrush(Colors.Red));
}
else if (item.mark > 50 && item.mark < 80)
{
model1.CustomBrush.Add(new SolidColorBrush(Colors.Blue));
}
}
ChartColorModel model = new ChartColorModel();
model.CustomBrushes = model1.CustomBrush;
return model;
}
Regards,
Sheik
Related
how to change gridview row color based on condition in uwp c#?
I want to highlight the gridview row based on my conditon.
A convenient way to do this would be to put a Border around your GridViewItem and use a ValueConverter to choose the background color based on the current item.
First you define your value converter:
public class ItemToColorConverter: IValueConverter
{
//this converts the item from your data source to the color brush
//of the background of the row
public object Convert(object value, Type targetType,
object parameter, string language)
{
//cast the value parameter to the type of item in your data source
var yourValue = ( YourType )value;
if ( yourValue > 10 ) //some condition you want to use to choose the color
{
//highlight
return new SolidColorBrush( Colors.Green );
}
else
{
//leave no background
return new SolidColorBrush( Colors.Transparent );
}
}
//you don't have to implement conversion back as this is just one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
}
Now you need to create a Application resource instance of the converter in App.xaml:
<Application ...>
<Application.Resources>
<converters:ItemToColorConverter x:Key="ItemToColorConverter" />
</Application.Resources>
</Application>
Now use this converter in your GridView item DataTemplate:
<GridView ItemsSource="{Binding YourDataSource"}>
<GridView.ItemTemplate>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource ItemToColorConverter}">
<!-- ... your content -->
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
I am working in a Windows Phone 8.1 application (non SL) and have a property on my ViewModel called Apples which has a value of 3.
I want a repeater to draw x Apples (I have the image) where x is the value of Apples on my ViewModel.
How can I achieve this in XAML? I have the following at the moment:
<ItemsControl Grid.Column="0" ItemsSource="{Binding Lives, Converter={}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- image here -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I am guessing I need to somehow convert the integer into some sort of items?
ItemsSource="{Binding SomeNumber, Converter={StaticResource NumberToItemsConverter}}"
public class NumberToItemsConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var number = (int)value;
return Enumerable.Range(1, number);
}
}
You have two options:
Option one
You need to create a list of apples, and bind the ItemsControl to it.
Here's an example:
int x = 3;
Apples = new ObservableCollection<Apple>();
/*your ViewModel must implement INotifyPropertyChanged so the UI will be
notified by the changes in the Apples list*/
for(int i = 0; i < x; i++)
{
Apples.Add(new Apple());
}
//now your ItemsControl has 3 apples
Option two
You create a custom control that does what I wrote above, in the code behind. In that control you create a dependency property of type int, and when that property is changed you can add items to your ItemsControl.
What if you try it out with a GridView with 5 items (each of them styled as an apple). The GridView has its source backed by with ObservableCollection. When a life is "consumed", just remove an item from the collection.
I ran into a weird problem working with the Windows Phone 8 pivot control.
Apparently, when the Pivot is bound to a list of models and the HeaderTemplate is used for binding to one of the model's properties, changing the property during runtime causes layout problem in the headers.
Here is the sample code.
Create a simple model class.
public class MyModel: INotifyPropertyChanged
{
private string _displayName;
public event PropertyChangedEventHandler PropertyChanged;
public string DisplayName
{
get
{
return _displayName;
}
set
{
_displayName = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DisplayName"));
}
}
}
}
Then initialize the model list (could occur in the ViewModel, or page's code)
Items = new List<MyModel>
{
new MyModel { DisplayName = "model 1" },
new MyModel { DisplayName = "model 2" },
}
Connecting the list of models to the pivot control
<phone:Pivot ItemsSource="{Binding Items}" DisplayMemberPath="DisplayName">
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}" />
</DataTemplate>
</phone:Pivot.HeaderTemplate>
</phone:Pivot>
So now we have 2 pivot items and the headers are displayed correctly. Now we change DisplayName property of the first item during app runtime (say following a button click) and assign a longer string value:
Items[0].DisplayName = "Some other header";
This causes the headers to overlap.
Any thoughts?
Elad
if you set displayName property to "Some other header" at first time, and then change the value to "model 2", normal display. So, in my opinion, this problem was caused by the pivot header width property(in your code,the textblock's width). When the pivot was loaded, every header had fixed width, and at this time, you changed the header display name to a longger value, the pivot will not be display correctly.
I want To access named controls inside listview datatemplet i followed this: How to Access a Named Control Inside a XAML DataTemplate (using CSharp
itemlistview.ItemsSource=new List<MyObject>();
foreach (var item in itemListView.Items)
{
var _Container = itemListView.ItemContainerGenerator
.ContainerFromItem(item);
var _Children = AllChildren(_Container);
var _FirstName = _Children.OfType<StackPanel>()
.First(x => x.Name.Equals("subjectListItem"));
_FirstName.Visibility =
Visibility.Collapsed;
}
But the problem in var item in itemListView.Items it retruns the MyObject which i passed to listview ItemsSource Not the controles inside the datatemplete.
So How can I return The Controls ?
ItemContainerGenerator.ContainerFromItem(object) should return an instance of ListViewItem that contains the data of the item.
But, this practice is discourage in WPF/WinRT frameworks. The "right" way is to control the data and have the UI react to it.
Something like:
// in code
var list = new List<MyObject>();
foreach (var item in list)
{
item.IsSomethingVisible = false;
}
itemlistview.ItemsSource = list;
<!--in XAML-->
<StackPanel Visibility="{Binding IsSomethingVisible,
Converter={StaticResource BooleanToVisibilityConverter}}".../>
<!--The converter is defined in resources of some higher level-->
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
I have a ColumnSeries chart where I want to control the selected item from the view model. I do this by binding the the SelectedItem of the chart to an object on the view model.
<chartingToolkit:Chart Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="0" MinHeight="200" Margin="0" x:Name="ratingsChart" Style="{StaticResource ChartWithoutLegendStyle}">
<chartingToolkit:Chart.Series>
<chartingToolkit:ColumnSeries x:Name="chartRatingColSeries" IsSelectionEnabled="True"
SelectedItem="{Binding SelectedRatingDistribution, Mode=TwoWay}"
ItemsSource="{Binding RatingsList}"
IndependentValueBinding="{Binding RatingName}"
DependentValueBinding="{Binding NumberOfGoodies}">
</chartingToolkit:ColumnSeries>
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>
There are various elements on the page which will force the chart's data to be reloaded (via a web service). When I need to reload the charts data (from the view model), I want to set the SelectedItem of the chart to the very first data point. This appears to work EXCEPT the chart does not visually show (in red, by default) the selected item. Here is sample code that reloads data after web service call and resets selected item:
private RatingDistribution _selectedRatingDistribution = new RatingDistribution();
public RatingDistribution SelectedRatingDistribution
{
get { return _selectedRatingDistribution; }
set
{
_selectedRatingDistribution = value;
RaisePropertyChanged("SelectedRatingDistribution");
}
}
private ObservableCollection<RatingDistribution> _lstRatings = new ObservableCollection<RatingDistribution>();
public ObservableCollection<RatingDistribution> RatingsList
{
get { return _lstRatings; }
set
{
_lstRatings = value;
RaisePropertyChanged("RatingsList");
}
}
private void GetRatingsDistributionCompleted(object sender, GetRatingsDistributionCompletedEventArgs e)
{
IsBusy = false;
RatingsList.Clear();
foreach (RatingDistribution rd in e.Result)
RatingsList.Add(rd);
SelectedRatingDistribution = RatingsList[0];
}
Setting the SelectedRatingDistribution from the View model will not visually show the selected item on the chart in red. Any ideas??
Update:
So if I click on a column, the chart correctly shows the selected item in red as so:
But If I set the SelectedItem from view model, the column will not be displayed in red (as the selected item)
Changed function below fixes problem..
private void GetRatingsDistributionCompleted(object sender, GetRatingsDistributionCompletedEventArgs e) {
RatingsList.Clear();
foreach (RatingDistribution rd in e.Result)
RatingsList.Add(rd);
App.Current.RootVisual.Dispatcher.BeginInvoke(new Action(delegate() { SelectedRatingDistribution = RatingsList[0]; }));
}