我正在使用C#-WPF。重新評估UserControl中的屬性
我做了我自己的UserControl,一個簡單的十字。通過座標,我可以在圖像上繪製一個X.
參數爲:
- CenterPoint的:中心的橫
- 前景:色爲橫
- 厚度:厚度爲十字
我Cross.xaml:
<UserControl x:Name="userControl"
x:Class="Project.Cross">
<Grid>
<Line Stroke="{Binding Foreground, ElementName=userControl}"
StrokeThickness="{Binding Thickness, ElementName=userControl}"
X1="{Binding X1, ElementName=userControl, Mode=OneWay}"
X2="{Binding X2, ElementName=userControl, Mode=OneWay}"
Y1="{Binding Y1, ElementName=userControl, Mode=OneWay}"
Y2="{Binding Y2, ElementName=userControl, Mode=OneWay}" />
<Line Stroke="{Binding Foreground, ElementName=userControl}"
StrokeThickness="{Binding Thickness, ElementName=userControl}"
X1="{Binding X2, ElementName=userControl, Mode=OneWay}"
X2="{Binding X1, ElementName=userControl, Mode=OneWay}"
Y1="{Binding Y1, ElementName=userControl, Mode=OneWay}"
Y2="{Binding Y2, ElementName=userControl, Mode=OneWay}"/>
</Grid>
</UserControl>
我Cross.xaml.cs:
public partial class Cross : UserControl
{
public Cross()
{
InitializeComponent();
}
public readonly static DependencyProperty CenterPointProperty = DependencyProperty.Register("CenterPoint",
typeof(PointF), typeof(Cross),
new PropertyMetadata(default(PointF)));
public PointF CenterPoint
{
get { return (PointF)GetValue(CenterPointProperty); }
set { SetValue(CenterPointProperty, value); }
}
public readonly static DependencyProperty ThicknessProperty = DependencyProperty.Register("Thickness",
typeof(int), typeof(Cross),
new PropertyMetadata(2));
public int Thickness
{
get { return (int)GetValue(ThicknessProperty); }
set { SetValue(ThicknessProperty, value); }
}
public float X1
{
get
{
return (float)(CenterPoint.X - (Width/2));
}
}
public float X2
{
get
{
return (float)(CenterPoint.X + (Width/2));
}
}
public float Y1
{
get
{
return (float)(CenterPoint.Y - (Height/2));
}
}
public float Y2
{
get
{
return (float)(CenterPoint.Y + (Height/2));
}
}
}
我可以把它想:
<local:Cross CenterPoint="{Binding Point}" Thickness="8" Foreground="Yellow" Height="40" Width="40"/>
我有一個問題,則不會顯示在十字架上。我添加了斷點,看起來X1,X2,...在更改CenterPoint時不會刷新。我如何強制C#重新評估這些值? (希望這解決了我的問題)
謝謝
您可以顯示本地的xaml:跨定義?例如它包含什麼? –
我不明白你的問題。我展示了Cross.xaml文件,我在本地命名空間中定義了UserControl'Cross'。 –
我只是想知道爲什麼不根據用戶控件的寬度和高度繪製交叉爲固定大小,然後通過設置其相對於目標對象的位置來移動該對象。所以我想知道你在畫什麼。 –