XAML绑定值

迈克尔24B

我正在开发Windows Phone 8.1应用程序,我需要圆形的progressbar。

我有此用户控制代码:

<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”的值?

仁迪申

如果您需要myValue从MainPage中获取您正在使用的位置UserControl,则可以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); }
    }
}

并在您的MainPage上这样:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章