Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
60
Binding Brush property to a property inside of a List of Dictionary
posted

Hi all,

I have a ViewModel class with this public property:
public List<Dictionary<int, DataModel>> Data { get; set; }

where:

DataModel{
public double X { get; set; }
public int val {get; set;}
public string Color
{
get
{
return Legend.getColor(val); //return a string like "red" or "green", etc....
}
}

---------------------------

XAML file:
<ig:XamDataChart x:Name="BarChart">

<ig:XamDataChart.Axes>
<ig:NumericXAxis x:Name="XAxis"/>
<ig:CategoryYAxis x:Name="YAxis" ItemsSource="{Binding Data}"/>
</ig:XamDataChart.Axes>

<ig:XamDataChart.Series>
<ig:StackedBarSeries
x:Name="StackedSeries" ItemsSource="{Binding Data}"
XAxis="{x:Reference XAxis}"
YAxis="{x:Reference YAxis}" Brush="{Binding [Value].Color}"> <!-- Brush property doesn't work -->
</ig:StackedBarSeries>
</ig:XamDataChart.Series>

</ig:XamDataChart>

----------------
Code-behind:

CB = new ViewModel();
BarChart.BindingContext = CB;

for (int k = 0; k < CB.maxArraySize ; k++)
{
string key = "[" + k.ToString() + "]";
StackedFragmentSeries seriesFragment = new StackedFragmentSeries();
seriesFragment.ValueMemberPath = key+"[X]";
StackedSeries.Series.Add(seriesFragment);
}
-----------------

I want to bind the Brush property of the StackedSeries to the Color property of DataModel.
I would like that each fragment of each series has a chosen color.
Actually, if I don't set the Brush property, all first fragments have the same green color, all second fragments have the same cyan color, etc... like the image in attachment.
The "behavior" of Brush property to get Color value, should be the same of ValueMemberPath.


I tried with
Brush="{Binding [Value].Color}" // List<Dictionary<int, DataModel>> where DataModel has Color property.
but it doesn't work.

Any ideas please?


Thanks to all for your support.

  • 27900
    Verified Answer
    Offline posted

    Hello Giuseppe,

    Unfortunately, there does not currently exist a way to do this at the moment. The Brush property on the StackedBarSeries object does not do anything, as it expects the Brush property of the StackedFragmentSeries to provide the coloring for the rectangles that are drawn to the XamDataChart. This does not set the colors of the rectangles separately, though. It will blanket each of the rectangles that the StackedFragmentSeries represents in a single color.

    For example, if you were to set the Brush property of your first StackedFragmentSeries to a new Infragistics.XamarinForms.SolidColorBrush, I imagine all of the light-green elements in your screenshot would turn Red.

    It is also worth noting that the current way that you are trying to bind this Brush will not work. The StackedColumnSeries and the StackedFragmentSeries' BindingContexts are not the items that they are representing, but they share the BindingContext from the owning XamDataChart unless set otherwise. You would also need to bind to an Infragistics.XamarinForms.SolidColorBrush element, and not a string for this to work correctly.

    Normally, I would recommend digging into the "visual tree" of the XamDataChart in order to achieve this, and obtain the Rectangle visual elements that are used, but in the Xamarin.Forms XamDataChart, no visual Rectangles are actually used. We are using SkiaSharp to essentially draw straight onto the Canvas, and so the only way coloring of the individual "rectangles" would be possible at the moment is if we exposed something that let you catch them just before they are actually drawn. Perhaps we may be able to do something with the AssigningCategoryStyle event as mentioned in another discussion we were having here: https://www.infragistics.com/community/forums/f/ultimate-ui-for-xamarin/111823/bar-series-with-different-style-chunks.

    Unfortunately, this is not currently exposed or implemented, and if you would like to see it exposed, I would recommend suggesting a new product idea for it by e-mailing ideas@infragistics.com. This product ideas site will place you in direct communication with our product management teams who plan and prioritize upcoming features and development based on community and user feedback.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer