简体   繁体   中英

C# WPF MVVM - Binding VisibilityOnLegend or Visibility don't work

The binding on the chart:FastLineBitmapSeries which is part of the SyncFusion package does not work.

How to do it? I have another property that uses the BoolToVisibility converter that works, why not this one?

Here is my code:

Xaml

<chart:FastLineBitmapSeries DataContext="{Binding AllSeries[VT8PvPower]}" Interior="#7f84e8" VisibilityOnLegend="{Binding AllSeriesVisibility[VT10PvPower], Converter={StaticResource BoolToVisibility}, Mode=TwoWay}" />

The DataContext is working fine but not the VisibilityOnLegend

View model

AllSeriesVisibility propertie: (same as AllSeries)

public ObservableDictionary<string, bool> AllSeriesVisibility { get; set; } = new ObservableDictionary<string, bool>();

Content of the properties:

AllSeriesVisibility - Keys

AllSeriesVisibility - 键

Values:

价值观

  • the values are not always at true like in pic

Converter

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is bool bValue))
            return Visibility.Hidden;
        return bValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Change the binding modes / change the public property, I tried to put a public bool instead of an ObservableDictionnary but it still doesn't work

We have analyzed your code snippet, we suspect that the classes that contain the AllSeries and AllSeriesVisibility properties are the same.

The remaining binding properties for the series should be in the AllSeries value object when you specify BindingContext as the AllSeries Key object, not in the AllSeries parent class.

Based on your code snippet, we created a simple sample that functions properly when given the right class structure as shown below. Additionally, VisibilityonLegend will function flawlessly.

<chart:SfChart x:Name="Chart" Margin="10">

 

            <chart:SfChart.Resources>

                <local:VisibilityConverter x:Key="BoolToVisiblity"/>

            </chart:SfChart.Resources>

              . . .

            <chart:FastLineBitmapSeries DataContext="{Binding AllSeries[Series1]}"

                                                                ItemsSource="{Binding ChartData}" Label="series1"

                                                                VisibilityOnLegend="{Binding AllSeriesVisibility[Series1],

                                                                Converter={StaticResource BoolToVisiblity}}"

                                                                XBindingPath="XValue" YBindingPath="YValue1" />

            <chart:FastLineBitmapSeries DataContext="{Binding AllSeries[Series2]}"

                                                                ItemsSource="{Binding ChartData}" Label="series2"

                                                                VisibilityOnLegend="{Binding AllSeriesVisibility[Series2],

                                                                Converter={StaticResource BoolToVisiblity }}"

                                                                XBindingPath="XValue" YBindingPath="YValue2" />

              . . .

</chart:SfChart>



public class ViewModel

{       

        public Dictionary<string, ViewModel1> AllSeries { get; set; } =

        new Dictionary<string, ViewModel1>();

        public string Series1Name { get; set; } = "Series1";

        public string Series2Name { get; set; } = "Series2";

        public ViewModel()

        {

            AllSeries["Series1"] = new ViewModel1(false);

            AllSeries["Series2"] = new ViewModel1(true);

        }

}

 

public class ViewModel1

{

        private ObservableCollection<DataPoint> _chartData;

        public ObservableCollection<DataPoint> ChartData

        {

            get { return _chartData; }

            set { _chartData = value; }

        }

        public Dictionary<string, bool> AllSeriesVisibility { get; set; } = new Dictionary<string, bool>();

        public ViewModel1(bool value)

        {

            AllSeriesVisibility["Series1"] = value;

            AllSeriesVisibility["Series2"] = value;

            var vTemp = new ObservableCollection<DataPoint>();

            var random = new Random();

            for (var i = 1; i < 15; i++)

            {

                vTemp.Add(new DataPoint { XValue = i, YValue1 = random.NextDouble(),

                                          YValue2=random.NextDouble() });

            }

            ChartData = vTemp;

        }

}

Please check this and let us know if you need any further assistance.

Regards, Muneesh Kumar G

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM