如何绑定到WPF中的另一个控件属性

我通过命名空间使用WPF Charting ToolKit:

xmlns:ChartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:VisualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" 

我有一个图表控件,在每次进行Lineseries绘图时我会生成一个随机颜色。 我删除数据点标记并使用以下样式着色

              

LineSeries是通过定义的

  

现在,这可以正常工作但图例标记显示的颜色与我为LineSeries生成的随机颜色不同。 我想为LineSeriesLineseries本身的图例项目显示相同的颜色。 所以,我将传说项目设置如下

                

我的问题是如何“绑定” TestSuiteLegendItemStyle样式中的Rectangle.Fill ,以便它与LineDataPointStyle定义的LineDataPointStyle设置的LineSeries颜色相同? *

谢谢你的时间。


编辑。 我已经尝试设置一个DependencyProperty ,它保存我的情节的Background颜色,如下所示

  ...  ...  

我已经修改了转换器(标记为),以便我可以存储随机生成的背景颜色,然后在我的图例中使用它

 public class ColorToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Brush b = new SolidColorBrush(Utils.GenerateRandomColor()); MultiChartExtensions.BackgroundBrushProperty = b; <= how to set the dependency property? return b; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 

其中依赖属性(DP)定义为

 public static readonly DependencyProperty BackgroundBrushProperty; public static void SetBackgroundBrush(DependencyObject DepObject, Brush value) { DepObject.SetValue(BackgroundBrushProperty, value); } public static Brush GetBackgroundBrush(DependencyObject DepObject) { return (Brush)DepObject.GetValue(BackgroundBrushProperty); } 

然后我会通过此DP来设置图例背景

                

任何有关这方面的帮助将不胜感激……

Part I. Binding in ControlTemplate

如果要在ControlTemplate使用Binding ,则应使用以下构造:

   

引自MSDN

TemplateBinding是模板场景的绑定的优化forms,类似于使用{Binding RelativeSource={RelativeSource TemplatedParent}}.构造的{Binding RelativeSource={RelativeSource TemplatedParent}}.

Notes about using TemplateBinding

TemplateBinding在模板外或其VisualTree属性之外不起作用,因此您甚至无法在模板的触发器中使用TemplateBinding。 此外,当应用于Freezable时,TemplateBinding不起作用(主要是出于人为原因),例如VisualBrush 。 在这种情况下,可以像这样使用Binding:

  

此外,您始终可以使用TemplateBinding的替代方法:

  

作为另一种可能性,您还可以尝试以下方法:

  

Part II. Notes about your version

在您的情况下,这可能会导致ControlTemplate的名称冲突,因为您已经使用Binding背景是为Border。 因此,删除它为Border 绑定 ,或使用其他属性,如Tag附加依赖属性绑定背景颜色。

Example of using

取而代之的是ChartingToolkit控件,作为Button控件的基础,因为它更容易展示这种样式的想法。

Solution 1: using Tag

       

Output

在此处输入图像描述

对于Rectangle ,设置两种颜色: Rectangle默认值,Tag for Border 。 我觉得这不是一个好的解决方案,原因如下:

  • 如果Border和Rectangle需要设置不同的值,例如:Background,BorderThickness,BorderBrush等,则一个Tag是不够的。

  • 有一个名称属性必须明确其目的,一个名称“标记”我们什么也没说。

在这些缺点中可以得出结论,我们应该找到一个替代方案,作为替代方案,我使用带有附加依赖属性的扩展类。

扩展程序类ButtonExt.cs

 public static class ButtonExt { #region RectangleBackground Property public static readonly DependencyProperty RectangleBackgroundProperty; public static void SetRectangleBackground(DependencyObject DepObject, Brush value) { DepObject.SetValue(RectangleBackgroundProperty, value); } public static Brush GetRectangleBackground(DependencyObject DepObject) { return (Brush)DepObject.GetValue(RectangleBackgroundProperty); } #endregion #region RectangleBorderBrush Property public static readonly DependencyProperty RectangleBorderBrushProperty; public static void SetRectangleBorderBrush(DependencyObject DepObject, Brush value) { DepObject.SetValue(RectangleBorderBrushProperty, value); } public static Brush GetRectangleBorderBrush(DependencyObject DepObject) { return (Brush)DepObject.GetValue(RectangleBorderBrushProperty); } #endregion #region Button Constructor static ButtonExt() { #region RectangleBackground PropertyMetadata BrushPropertyMetadata = new PropertyMetadata(Brushes.Transparent); RectangleBackgroundProperty = DependencyProperty.RegisterAttached("RectangleBackground", typeof(Brush), typeof(ButtonExt), BrushPropertyMetadata); #endregion #region RectangleBorderBrush RectangleBorderBrushProperty = DependencyProperty.RegisterAttached("RectangleBorderBrush", typeof(Brush), typeof(ButtonExt), BrushPropertyMetadata); #endregion } #endregion } 

MainWindow.xaml

       

Output

在此处输入图像描述

Part III. Setting values for dependency properties

创建并注册附加的依赖项属性时,必须声明Set和Get方法与他一起使用:

 public static void SetRectangleBackground(DependencyObject DepObject, Brush value) { DepObject.SetValue(RectangleBackgroundProperty, value); } public static Brush GetRectangleBackground(DependencyObject DepObject) { return (Brush)DepObject.GetValue(RectangleBackgroundProperty); } 

然后与他们合作将如下:

Set

 ButtonExt.SetRectangleBackground(MyButton, Brushes.Red); 

Get

 Brush MyBrush = ButtonExt.GetRectangleBackground(MyButton); 

但就我们而言,并非如此简单。 当我使用附加的依赖属性问题时,更新值不是。 但在我们的例子中,属性在模板中,在我的情况下没有更新Button 。 我试图在Binding和属性声明中设置Mode=TwoWayUpdateSourceTrigger=PropertyChangedGetBindingExpression().UpdateTarget() ,但它没用。

请注意,对于属性设置值和模板的通知,属性已更新。 也许我错了,你会工作,或者可能是专门制作的,例如为了避免内存泄漏。

在任何情况下,最好不要直接更新依赖属性,并将Model的属性绑定到它,并在ViewModel中设置值。

例:

  

其中RectBackgroundRectBorderBrush实现INotifyPropertyChanged接口。

作为这种情况的替代方法,请不要使用依赖项属性并将DataTemplate用于控件。 DataTemplate是MVVM的理想选择,非常灵活和动态。

例如,使用DataTemplate ,您可以看到我的答案:

创建(创建)可重用的动态视图

一个用于UserControl和Window的ViewModel或单独的ViewModel

在LineSeries控件上设置x:Name

  

然后,您可以使用ElementName通过绑定绑定TestSuiteLegendItemStyle