Skip to main content
 首页 » 编程设计

c#之如何绑定(bind)要在 xaml 中使用的值

2024年08月06日36mate10pro

我是 Xaml 的新手(我在其中进行的第一个项目),希望得到一些帮助。我正在尝试制作一个径向量规以保存在 .dll 中以用于其他项目,但在尝试传递针的角度值时遇到了困难。 最后,我希望能够像调用方法一样调用给出角度的量规。

例如:
<RadialGauge Angle="50" HorizontalAlignment="Left" Margin="532,278,0,0" VerticalAlignment="Top" />

不过,我在发送要由我的仪表使用的值时遇到了问题,我们将不胜感激。

我的.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> 

我的.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
 
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236 
 
namespace DTIGauge 
{ 
    public sealed partial class RadialGauge 
    { 
 
        Int32 temp; 
        public RadialGauge(Int32 myInt) 
        { 
            this.InitializeComponent(); 
            temp = myInt; 
 
        } 
        Int32 _myint; 
        public Int32 the_angle 
        { 
            get { return this._myint; } 
            set { this._myint = temp; } 
        } 
 
    } 
} 

请您参考如下方法:

您需要在 UserControl 的代码后面创建一个依赖属性 ( MSDN )。

依赖属性具有支持 WPF 中数据绑定(bind)的所有必要功能。

您可以修改后面的代码以包含此属性而不是 Angle 属性:

// 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}"/> 

绑定(bind)到这个属性。

Visual Studio 中有一个方便的代码片段可以帮助创建此代码。只需键入“propdp”并点击 Tab 两次。

要了解有关依赖属性以及绑定(bind)在 WPF 中如何工作的更多信息,请查看此网站:http://wpftutorial.net/DependencyProperties.html

PS:我建议为 Angle 使用 Double 值,而不是 Int。

祝你好运:)