我在wpf中有两个自定义控件。一个是图表控件,另一个是循环进度条。图表控件具有以下相关代码。
> public int BarValue { get; set; }
> private void DrawData()
> {
> string data = File.ReadAllText(DataSource);
> string[] valueData = data.Split(';');
> string[] strValues = valueData[0].Split(',');
> string[] strYear = valueData[1].Split(',');
> double section = 525 / strValues.Length;
> rectSpace = (section * 20) / 100;
> rectWidth = (section * 80) / 100;
>
>
> for (int i = 0; i < strYear.Length; i++)
> {
>
> Rectangle rec = new Rectangle();
> rec.Width = rectWidth;
> rec.Height = Convert.ToDouble(strValues[i]);
>
> rec.Margin = new Thickness((rectSpace), (350 - rec.Height),
> 0,25);
> rec.Fill = BarsColor;
> panel.Children.Add(rec); rec.MouseDown += Rec_MouseDown; }
private void Rec_MouseDown(object sender,
> MouseButtonEventArgs e)
> {
> BarValue =Convert.ToInt32( ((Rectangle)sender).Height);
> }
然后ProgressBar有一个属性Value。 我将两个控件添加到MainWindow。如果我点击一个上面提供的mouseDown事件的矩形,我怎么能改变ProgressBar的valueproperty。 我试过了
progCircle.Value = chart.BarValue;
XML的主
<Grid Margin="1,0,-318,-30" HorizontalAlignment="Center" Width="1418">
<Rectangle x:Name="MainRec" RadiusX="9" RadiusY="9" Margin="25,38,425,51" Opacity="0.75">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF0F2A3C" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="TitleBar" Fill="#FF070E17" HorizontalAlignment="Left" Height="40" Margin="25,38,0,0" RadiusY="9" RadiusX="9" VerticalAlignment="Top" Width="968" Opacity="0.75" MouseDown="TitleBar_MouseDown"/>
<Label Content="Wools Valley" Foreground="Wheat" Margin="43,42,1225,565" Width="150" Height="30" FontSize="14"/>
<Ellipse x:Name="closeButton" HorizontalAlignment="Left" Height="20" Margin="960,52,0,0" VerticalAlignment="Top" Width="20" MouseDown="closeButton_MouseDown" Opacity="0.7" ToolTip="Close" RenderTransformOrigin="4.851,0.607">
<Ellipse.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="DarkCyan" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Ellipse.Fill>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="closeButton"
Storyboard.TargetProperty="Opacity"
From="0.7" To="1" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
<BarGraph2:BarGraph x:Name="bargraph" HorizontalAlignment="Left" Height="425" Margin="79,99,0,0" VerticalAlignment="Top" Width="654" DataSource="D:\\data3.txt" BarsColor="DarkCyan" AxisValueColor="Cyan" titleText="GDP Comparision" />
<ControlExamplesVS:ProgressCircle x:Name="progCircle" HorizontalAlignment="Left" Height="185" Margin="760,99,0,0" VerticalAlignment="Top" Width="184" IndicatorBrush="Cyan" BackgroundBrush="Transparent" ProgressBorderBrush="DarkCyan"/>
</Grid>
答案 0 :(得分:0)
如果您想在后面的代码中执行此操作,请在MainWindow中添加控件后添加以下代码:
Binding bind= new Binding();
bind.Source = chart;
bind.Path = new PropertyPath("BarValue");
bind.Mode = BindingMode.TwoWay;
bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(progCircle, CircuòarProgressBar.ValueProperty, bind);
如果您已将它们放在xaml中,则只需执行以下操作:
<CircularProgressBar Value="{Binding ElementName = chart, Path = Value}"/>
<Chart x:Name = "chart"/>