[英]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.图表上的绑定:作为 SyncFusion package 一部分的 FastLineBitmapSeries 不起作用。
How to do it?怎么做? I have another property that uses the BoolToVisibility converter that works, why not this one?我有另一个使用 BoolToVisibility 转换器的属性,为什么不使用这个呢?
Here is my code:这是我的代码:
Xaml 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 DataContext 工作正常但不是 VisibilityOnLegend
View model查看model
AllSeriesVisibility propertie: (same as AllSeries) AllSeriesVisibility 属性:(与 AllSeries 相同)
public ObservableDictionary<string, bool> AllSeriesVisibility { get; set; } = new ObservableDictionary<string, bool>();
Content of the properties:属性内容:
AllSeriesVisibility - Keys AllSeriesVisibility - 键
Values:价值观:
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更改绑定模式/更改公共属性,我尝试使用 public bool 而不是 ObservableDictionnary 但它仍然不起作用
We have analyzed your code snippet, we suspect that the classes that contain the AllSeries and AllSeriesVisibility properties are the same.我们已经分析了您的代码片段,我们怀疑包含 AllSeries 和 AllSeriesVisibility 属性的类是相同的。
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.当您将 BindingContext 指定为 AllSeries 键 object 时,该系列的其余绑定属性应在 AllSeries 值 object 中,而不是在 AllSeries 父 class 中。
Based on your code snippet, we created a simple sample that functions properly when given the right class structure as shown below.根据您的代码片段,我们创建了一个简单的示例,在给出正确的 class 结构时可以正常运行,如下所示。 Additionally, VisibilityonLegend will function flawlessly.此外,VisibilityonLegend 将 function 完美无缺。
<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问候, Muneesh Kumar G
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.