2017-01-19 39 views
1

我有一个WPF项目,并在列表框中使用ProgressBars来显示一系列值。我想在位于进度条正上方的文本框中显示数值。我怎样才能得到每个进度条的Value的位置,然后我可以放置一个TextBox? 这是ProgressBar xaml:如何获取进度条的当前值的位置

<ProgressBar Orientation="Vertical" 
          Width="78" 
          BorderThickness="0" 
          Minimum="{Binding minTest}" 
          Maximum="{Binding maxTest}" 
          Value="{Binding valTest}" 
          Background="Transparent" 
          Foreground="{Binding bar_color}"/> 
+0

你的意思是在屏幕上的酒吧在哪里的X位置?这只是(值*宽度)/(最大 - 最小),不是? – SledgeHammer

+0

您是否要求在最小值和最大值之间移动的百分比? (也可计算) – BradleyDotNET

+0

是,进度条所在位置的屏幕上的X位置,分别位于最小值和最大值之间。我在想可能有一个属性,我可以查询,但我也可以计算它。谢谢。 – user94639

回答

2

有几种方法可以做到这一点。一个可能是:

<Window x:Class="SO41749207.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:SO41749207" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
      <StackPanel Orientation="Vertical"> 
       <TextBox Text="{Binding ElementName=MyProgress, Path=Value}" /> 
       <ProgressBar Name="MyProgress" Value="50" /> 
      </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </Window.Resources> 
    <Grid> 
    <ListBox> 
     <ListBoxItem>a</ListBoxItem> 
     <ListBoxItem>b</ListBoxItem> 
     <ListBoxItem>c</ListBoxItem> 
    </ListBox> 
    </Grid> 
</Window> 

更MVVM式的方法是定义列表项的视图模型:

public class MyListItem : INotifyPropertyChanged 
    { 

    double m_progressValue = 0.0; 
    public double ProgressValue 
    { 
     get { return m_progressValue; } 
     set 
     { 
     m_progressValue = value; 
     OnPropertyChanged("ProgressValue"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string property) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 
    } 
    } 

...和相应的DataTemplate:

<DataTemplate DataType="{x:Type models:MyListItem}"> 
    <StackPanel Orientation="Vertical"> 
    <TextBox Text="{Binding ProgressValue}" HorizontalAlignment="Center" Width="50" TextAlignment="Center" /> 
    <ProgressBar Value="{Binding ProgressValue}" /> 
    </StackPanel> 
</DataTemplate> 

...对于列表项目:

<ListBox HorizontalContentAlignment="Stretch"> 
    <ListBox.ItemsSource> 
    <cols:ArrayList> 
     <models:MyListItem ProgressValue="10" /> 
     <models:MyListItem ProgressValue="50" /> 
     <models:MyListItem ProgressValue="80" /> 
    </cols:ArrayList> 
    </ListBox.ItemsSource> 
</ListBox> 
0

由于您已将ProgressBar值绑定到您的视图模型:

<ProgressBar Orientation="Vertical" 
         Width="78" 
         BorderThickness="0" 
         Minimum="{Binding minTest}" 
         Maximum="{Binding maxTest}" 
         Value="{Binding valTest}" 
         Background="Transparent" 
         Foreground="{Binding bar_color}"/> 

为什么不将TextBox绑定到相同的值?

<TextBox Text="{Binding valTest}" /> 
+0

这个建议设置了文本的值,但是我想要做的是设置文本框的位置。 – user94639