简体   繁体   中英

XAML binding value

I am developing windows phone 8.1 app and I need circular progressbar.

I have this usercontrol code:

<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>

How can I change value with the name "myValue" from code behind (for example, from MainPage.xaml.cs) and not from CircularProgressBarViewModel?

If you need to get myValue from your MainPage, where you are using your UserControl you can create DependencyProperty in your control and set any value from page to control.

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); }
    }
}

And on the your MainPage like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM