2017-01-19 14 views
1

私はWPFプロジェクトを持っており、ListBoxの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と最小と最大の間のX位置です。私はクエリできるプロパティがあるかもしれないと思っていたが、同様に計算できる。ありがとう。 – user94639

答えて

2

いくつかの方法があります。よりMVVMのようなアプローチは、リスト項目のビューモデルを定義することです

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

:一つは、可能性があり

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

関連する問題