XAML binding value

本文关键字:value binding XAML | 更新日期: 2023-09-27 18:35:26

我正在开发Windows Phone 8.1应用程序,我需要循环进度条。

我有这个用户控制代码:

<UserControl.Resources>
  <Style TargetType="ProgressBar" x:Key="CircularProgressBarStyle">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ProgressBar">
          <Grid x:Name="LayoutRoot">
            <local:CircularProgressBarViewModel.Attach>
              <local:CircularProgressBarViewModel HoleSizeFactor="0.75"/>
            </local:CircularProgressBarViewModel.Attach>
            <Ellipse Width="{Binding Diameter}" Height="{Binding Diameter}"
                       HorizontalAlignment="Center" VerticalAlignment="Center"
                       Stroke="LightGray" Opacity="0.5" Fill="Transparent"
                       StrokeThickness="10">
          </Ellipse>
            <local:PiePiece CentreX="{Binding CentreX}" CentreY="{Binding CentreY}"
                              RotationAngle="0" WedgeAngle="{Binding Angle}"
                              Radius="{Binding Radius}" InnerRadius="{Binding InnerRadius}"
                              Fill="Black" Opacity="0.7"/>
            <Grid util:GridUtils.RowDefinitions="*,2*,*"
                    util:GridUtils.ColumnDefinitions="*,2*,*">
              <Viewbox Grid.Row="1" Grid.Column="1">
                <TextBlock Name="myValue" Text="{myValue value}"
                             Foreground="WhiteSmoke"
                             FontWeight="Bold"
                             VerticalAlignment="Center" HorizontalAlignment="Center"/>
              </Viewbox>
            </Grid></Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </UserControl.Resources>

如何从代码隐藏(例如,从 MainPage.xaml.cs)而不是从 CircularProgressBarViewModel 更改名称为"myValue"的值?

XAML binding value

如果需要从使用UserControl的主页获取myValue,则可以在控件中创建DependencyProperty,并将页面到控件的任何值设置为控件。

public sealed partial class YourUserControl: UserControl
{
    public static readonly DependencyProperty myValueProperty = DependencyProperty.Register("myValue", typeof(string), typeof(YourUserControl), new PropertyMetadata(string.Empty));
    public YourUserControl()
    {
        this.InitializeComponent();
    }
    public string myValue
    {
        get { return (string)GetValue(myValueProperty ); }
        set { SetValue(myValueProperty , value); }
    }
}

在您的主页上,如下所示:

<controls:YourUserControl myValue={Binding YourValue}/>