【问题标题】:XAML binding valueXAML 绑定值
【发布时间】:2015-02-23 05:16:46
【问题描述】:

我正在开发 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”的值?

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

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

    在你的主页上是这样的:

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

    【讨论】:

      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2017-12-15
      • 2011-02-28
      • 2014-03-06
      • 2011-07-15
      • 2011-09-14
      相关资源
      最近更新 更多