如何从代码隐藏到达我的控制器

时间:2015-05-31 10:31:35

标签: wpf progress-bar geometry

我有Circle ProgressBar响应我的Slider值已更改:

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="530,303,114,303">
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="153" Width="155">
            <DesignInControl:CircularProgressBar HorizontalAlignment="Center" VerticalAlignment="Center"
             SegmentColor="#FF878889" StrokeThickness="8" Percentage="100" />
            <DesignInControl:CircularProgressBar Name="circleVale" HorizontalAlignment="Center" VerticalAlignment="Center"
             Percentage="{Binding Value, ElementName=slider}" SegmentColor="#026873" StrokeThickness="8" />
        </Grid>
    </StackPanel>
    <Slider x:Name="slider" Maximum="100" Value="0" Width="200" Margin="597,185,227,495" />
    <Label Name="lblCircleProgress" Content="{Binding Value, ElementName=slider}" Margin="698,344,284,343" Foreground="White"

如何在代码隐藏中更改此值? 我试图给这个控件一个name属性(Name =“circleVale”)但由于某种原因我无法在代码隐藏中达到它。

2 个答案:

答案 0 :(得分:0)

您的circleVale控件应该可以在后面的代码中访问,但除此之外,我认为使用SetCurrentValue将保留绑定。

您可以从代码中更改进度条的任何其他属性吗?

答案 1 :(得分:0)

你的CircularProgressBar的属性百分比名为&#34; circleVale&#34;绑定到滑块的值。为了从后面的代码中更改它们,请尝试设置depdendency属性:

    public int SliderValue
    {
        get { return (int)GetValue(SliderValueProperty); }
        set { SetValue(SliderValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SliderValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SliderValueProperty =
        DependencyProperty.Register("SliderValue", typeof(int), typeof(MainWindow), new PropertyMetadata(0));

    private void btnChangeSliderValue_Click(object sender, RoutedEventArgs e)
    {
        SliderValue = 70;
    }

在构造函数中设置Window的DataContext:

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

这可以是你的XAML的一个例子:

    <StackPanel>
        <Slider x:Name="slider" Maximum="100" Value="{Binding SliderValue}" Width="200"/>
        <Button Click="btnChangeSliderValue_Click" HorizontalAlignment="Center" Content="Change slider value" Margin="5,10"/>
    </StackPanel>

这样您就不需要使用其名称访问滑块。使用绑定更容易,您可以逐步访问MVVM。