我仅用代码创建了一个自定义控件,我希望它的占用空间尽可能小,因为它将根据样式模板内网格中的值进行重复。目前它有时会起作用,我认为分配新值和旧值时是否会触发 DependencyProperty Changed 事件?我错过了什么,我感觉这是一些基本的东西。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace SectorRotationApp.Controls
{
public partial class CircleRepeater : StackPanel
{
public CircleRepeater()
{
}
public string Score
{
get => (string)GetValue(ScoreProperty);
set => SetValue(ScoreProperty, value);
}
public static readonly DependencyProperty ScoreProperty =
DependencyProperty.Register(
"Score",
typeof(string),
typeof(CircleRepeater),
new UIPropertyMetadata("0", new PropertyChangedCallback(OnScoreChanged)));
public double Size
{
get => (double)GetValue(SizeProperty);
set => SetValue(SizeProperty, value);
}
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register(
"Size",
typeof(double),
typeof(CircleRepeater),
new FrameworkPropertyMetadata(10.0));
protected static void OnScoreChanged(DependencyObject depo, DependencyPropertyChangedEventArgs depa)
{
if (depo is CircleRepeater circRep && !string.IsNullOrEmpty((string)depa.NewValue))
{
string Score = (string)depa.NewValue;
circRep.Children.Clear();
if (double.TryParse(Score, out double trigPct))
{
if (trigPct > 30)
{
for (int i = 2; i < 6; i++)
{
if (trigPct - (i * 10) > 0)
{
Ellipse myEllipse = new Ellipse();
myEllipse.Width = circRep.Size;
myEllipse.Height = circRep.Size;
myEllipse.Fill= new SolidColorBrush(Colors.DarkOliveGreen);
myEllipse.Margin = new Thickness(5, 5, 0, 5);
circRep.Children.Add(myEllipse);
}
else
{
break;
}
}
}
}
}
}
}
}
<Border BorderBrush="Black" BorderThickness="1" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2">
<Grid >
<Grid.Background>
<MultiBinding FallbackValue="Transparent" Converter="{StaticResource BytToColourConverter}">
<Binding Path="IsGain"/>
<Binding Path="BackColour"/>
<Binding Path="IsGrey"/>
<Binding Path="IsGreyGain"/>
</MultiBinding>
</Grid.Background>
<Grid.ToolTip>
<MultiBinding Converter="{StaticResource PeriodToName}">
<Binding Path="timePeriod"/>
<Binding ElementName="SectorRotationPro" Path="DataContext.pBase"/>
<Binding Path="AlgoShortString"/>
</MultiBinding>
</Grid.ToolTip>
<conts:CircleRepeater Orientation="Horizontal" Score="{Binding AlgoScore,UpdateSourceTrigger=PropertyChanged}">
</conts:CircleRepeater>
</Grid>
</Border>
我想我已经在人工智能搜索的帮助下回答了我自己的问题...将值类型从 string 更改为 int 似乎有所帮助 =>
“如果您对属性使用引用类型,则可能会发生这种情况, 例如集合或对象,并且您正在修改其 属性或项目,而不是替换整个实例。避免 为此,您应该为您的属性使用值类型,例如 int 或 bool,或者创建引用类型的新实例 每当您想更新属性值时”
https://stackoverflow.com/questions/31990339/propertychangedcallback-not- Called
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace SectorRotationApp.Controls
{
// [TemplatePart(Name = "PreText", Type = typeof(StackPanel))]
public partial class CircleRepeater : StackPanel
{
public CircleRepeater()
{
}
public int Score
{
get => (int)GetValue(ScoreProperty);
set => SetValue(ScoreProperty, value);
}
public static readonly DependencyProperty ScoreProperty =
DependencyProperty.Register(
"Score",
typeof(int),
typeof(CircleRepeater),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnScoreChanged)));
public double Size
{
get => (double)GetValue(SizeProperty);
set => SetValue(SizeProperty, value);
}
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register(
"Size",
typeof(double),
typeof(CircleRepeater),
new FrameworkPropertyMetadata(10.0));
protected static void OnScoreChanged(DependencyObject depo, DependencyPropertyChangedEventArgs depa)
{
if (depo is CircleRepeater circRep)
{
circRep.Children.Clear();
int trigPct = (int)depa.NewValue;
if (trigPct > 30)
{
for (int i = 2; i < 6; i++)
{
if (trigPct - (i * 10) > 0)
{
Ellipse myEllipse = new Ellipse();
myEllipse.Width = circRep.Size;
myEllipse.Height = circRep.Size;
myEllipse.Fill = new SolidColorBrush(Colors.DarkOliveGreen);
myEllipse.Margin = new Thickness(5, 5, 0, 5);
circRep.Children.Add(myEllipse);
}
else
{
break;
}
}
}
}
}
}
}