仅通过XAML将复选框绑定到元素可见性



我不明白为什么绑定不像这样工作?

<UserControl>
<StackPanel>
<CheckBox x:Name="CBTempA"  VerticalAlignment="Center" Margin="10,0,0,0"/>
<Label VerticalAlignment="Center">Temp A</Label>
</StackPanel>
<lvc:CartesianChart>
<lvc:CartesianChart.Series>
<lvc:LineSeries Values="{Binding TempAValues}" Visibility="{Binding ElementName=CBTempA, Path=IsChecked, Converter={StaticResource BoolToVisibility}}"/>
</lvc:CartesianChart.Series>
</lvc:CartesianChart>
</UserControl>

转换器BoolToVisibility将布尔值(IsChecked)转换为System.Windows.Visibility值。它可能看起来像这样:

public class BoolToVisibilityConverter : IValueConverter
{
// value is a parameter from xaml (IsChecked)
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return (bool) value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
}
return System.Windows.Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

相关内容

  • 没有找到相关文章

最新更新