C# 如何使绑定成为要在xaml中使用的值
我是Xaml的新手(我从事的第一个项目),希望能得到一些帮助。我正试图制作一个径向量规,保存在一个.dll中,以便在其他项目中使用,但在尝试传递针的角度值时遇到了麻烦。 最后,我希望能够调用给出角度的仪表,就像方法调用一样 例如:C# 如何使绑定成为要在xaml中使用的值,c#,xaml,C#,Xaml,我是Xaml的新手(我从事的第一个项目),希望能得到一些帮助。我正试图制作一个径向量规,保存在一个.dll中,以便在其他项目中使用,但在尝试传递针的角度值时遇到了麻烦。 最后,我希望能够调用给出角度的仪表,就像方法调用一样 例如: 我有麻烦发送一个值,由我的仪表使用,虽然,任何帮助将不胜感激 我的.xaml <UserControl x:Class="DTIGauge.RadialGauge" xmlns="http://schemas.microsoft.com/winfx/2006/
我有麻烦发送一个值,由我的仪表使用,虽然,任何帮助将不胜感激
我的.xaml
<UserControl
x:Class="DTIGauge.RadialGauge"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DTIGauge"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="450" Width="450"
>
<Grid>
<Ellipse Fill="White" Margin="30"/>
<Grid >
<Polygon Height="450" Width="450" Stroke="Red" StrokeThickness="2" Fill="Red" Points="225,37 220,228 225,240 230,228">
<Polygon.RenderTransform>
<RotateTransform CenterX="225" CenterY="225" Angle="{Binding Path=the_angle}"/>
</Polygon.RenderTransform>
</Polygon>
</Grid>
<TextBlock Text="0°" TextAlignment="Center" FontSize="15" Foreground="White" Height="450" Width="450" VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock.RenderTransform>
<RotateTransform CenterX="225" CenterY="225" Angle="0"/>
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Text="20°" TextAlignment="Center" FontSize="15" Foreground="White" Height="450" Width="450" VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock.RenderTransform>
<RotateTransform CenterX="225" CenterY="225" Angle="20"/>
</TextBlock.RenderTransform>
</TextBlock>
<!--...And a lot more markers-->
</Grid>
您需要使用要在xaml中绑定的对象设置usercontrol的datacontext。为了帮助您更好地理解它的工作原理,我应用MVVM模式为您介绍了此设计: 声明一个类,如下所示:
public class RadialGougeViewModel : INotifyPropertyChanged
{
private int _angle;
public int Angle
{
get
{
return _angle;
}
set
{
_angle = value;
OnPropertyChanged("Angle");
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
接下来,在用户控件中,按以下方式设置根网格的datacontext:
<UserControl x:Class="WpfApplication1.Gouge"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<YourNamespace:RadialGougeViewModel x:Key="RadialGougeViewModel "/>
</UserControl.Resources>
<Grid DataContext="{StaticResource RadialGougeViewModel}">
</Grid>
现在,您可以在网格内声明的控件中使用viemodel属性:
<Polygon Height="450" Width="450" Stroke="Red" StrokeThickness="2" Fill="Red" Points="225,37 220,228 225,240 230,228">
<Polygon.RenderTransform>
<RotateTransform CenterX="225" CenterY="225" Angle="{Binding Path=Angle}"/>
</Polygon.RenderTransform>
</Polygon>
如果您想详细了解此模式的工作原理,我建议您阅读本文:您需要在UserControl的代码后面创建一个依赖属性() 依赖项属性具有支持WPF中数据绑定的所有必要功能 您可以修改代码以包含此属性,而不是角度属性:
// Dependency Property
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register( "Angle", typeof(int),
typeof(RadialGauge), new FrameworkPropertyMetadata(0));
// .NET Property wrapper
public int Angle
{
get { return (int)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
您的XAML需要更新为:
<RotateTransform CenterX="225" CenterY="225" Angle="{Binding Path=Angle, ElementName=userControl}"/>
绑定到该属性
VisualStudio中有一个方便的代码片段可以帮助创建此代码。只需输入“propdp”并点击Tab两次
要了解更多关于依赖项属性以及WPF中绑定的工作原理,请访问以下网站:
PS:我建议使用双精度的角度值,而不是Int
祝你好运:)回答得很好!DP绝对是解决这个问题的方法。@BradleyDotNET谢谢你整理我的文字-iPad键盘太有用了:)没问题。您的帖子非常适合作为平板电脑用户:)
<RotateTransform CenterX="225" CenterY="225" Angle="{Binding Path=Angle, ElementName=userControl}"/>