2017-05-08 11 views
0

プロパティを持つTapeオブジェクトがあります。リストであるシンボルとすべてのシンボルオブジェクトには、文字列プロパティRepresentationがあります。ListBoxItemのプロパティをListBoxのItemsSourceに既にバインドされているプロパティにバインドする方法

今、別のテキストボックスにシンボル内のすべてのシンボルの表示を表示するListBoxがあります。ときに、ユーザーの編集今 enter image description here

:私はこれを取得

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     List<Symbol> symbols = (List<Symbol>)value; 
     List<TextBox> list = new List<TextBox>(); 
     list.Add(new TextBox()); 
     foreach (Symbol symbol in symbols) 
     { 
      TextBox textBox = new TextBox(); 
      textBox.Text = symbol.Representation; 
      list.Add(textBox); 
     } 
     list.Add(new TextBox()); 
     return list; 
    } 

これはすでに動作し、起動時に:

<Grid> 
    <Grid.Resources> 
     <l:TapeToTextBoxListConverter x:Key="TapeToTextBoxListConverter"/> 
    </Grid.Resources> 
    <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden"> 
     <ListBox x:Name="listBox" KeyboardNavigation.TabNavigation="Continue" ItemsSource="{Binding Path=Tape.Symbols, Converter={StaticResource TapeToTextBoxListConverter}, Mode=TwoWay}"> 
      <ListBox.ItemContainerStyle> 
       <Style> 
        <Setter Property="KeyboardNavigation.IsTabStop" Value="False" /> 
       </Style> 
      </ListBox.ItemContainerStyle> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <StackPanel.OpacityMask> 
          <LinearGradientBrush EndPoint="1, 0.5" StartPoint="0, 0.5"> 
           <GradientStop Color="#00000000" Offset="0"/> 
           <GradientStop Offset="1"/> 
           <GradientStop Color="#FF727272" Offset="0.1"/> 
           <GradientStop Color="#FF727272" Offset="0.9"/> 
          </LinearGradientBrush> 
         </StackPanel.OpacityMask> 
        </StackPanel> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBox.Resources> 
       <Style TargetType="{x:Type TextBox}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type TextBox}"> 
           <Grid> 
            <Border Width="{Binding ActualWidth, ElementName=parentElementName}" MinWidth="80" Height="80" BorderBrush="Black" BorderThickness="2" CornerRadius="5" Background="#FFBFBFBF" /> 
            <ScrollViewer Margin="0" x:Name="PART_ContentHost"/> 
           </Grid> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
        <Setter Property="FontFamily" Value="Arial Bold"/> 
        <Setter Property="FontSize" Value="60"/> 
        <Setter Property="TextWrapping" Value="Wrap"/> 
        <Setter Property="TextAlignment" Value="Center"/> 
        <Setter Property="VerticalContentAlignment" Value="Center"/>    
       </Style> 
      </ListBox.Resources> 
     </ListBox> 
    </ScrollViewer> 
</Grid> 

そしてTapeToTextBoxListConverter:私はすでに、これは以下のXAMLコードで動作するようになりましたTextBoxes私はテープオブジェクトを(またはその中のシンボルリストと正確に)更新したいです。 は、私はすでにテンプレートセッターで

<Setter Property="Text" Value="{Binding Path=Tape.Symbols}"/> 

と同様のものの様々なバリエーションを試してみましたが、何も効果の多くを持っていません。私がコンバータで変換機能を起動させるだけであれば、私はすでに満足していますが、これを動作させることさえできません。

答えて

0

コンバータを使用してテキストボックスのリストを返さないでください。それは不要です。

ビューモデルにList<Symbol>の代わりにObservableCollection<Symbol>を使用し、DataTemplateを使用して、値変換器の代わりにテキストボックスを作成します。 DataTemplateでテキストボックスを適切に作成すると、DataContextオブジェクトのRepresentationプロパティ(Symbol)にバインドするのが簡単です。ListBoxItemsSourceはこれらのコレクションです。

<ListBox 
    x:Name="listBox" 
    KeyboardNavigation.TabNavigation="Continue" 
    ItemsSource="{Binding Tape.Symbols}" 
    > 
    <ListBox.ItemContainerStyle> 
     <Style> 
      <Setter Property="KeyboardNavigation.IsTabStop" Value="False" /> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBox 
       Text="{Binding Representation}" 
       /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 

途中でクールなUIが表示されます。不透明度勾配効果が好きです。

-1

まず、あなたはそのバインディングを必要とします。また、あなたはそれが必要です:Mode = "TwoWay"。

コンバータのリストを作成する方法は間違っています。 ListBoxはそのバインディングを必要とします(コンバーターなし)。

次に、TextBoxを含むItemTemplate(DataTemplateを受け取る)を定義します。

このテキストボックスでは、セッターでの試行からバインドを設定します(今回はインスタンス自体でのみ)。今回は、Representationプロパティに直接バインドします。

<ListBox.ItemTemplate> 
    <DataTemplate> 
     <TextBox Text="{Binding Representation, Mode=TwoWay}"/> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

また、ListBoxにはそのScrollViewerは必要ありません。ListBoxにはすでに内部があります。

+1

'Mode = TwoWay'は' TextBox.Text'のデフォルトです。デフォルトを使用するように指示する必要はありません。 –

+1

また、あなたは「ItemTemplate(DataTemplateを受け取る)を定義する」ではありません。 「ItemTemplate」タイプはありません。 ItemTemplateは、DataTemplateを割り当てるプロパティの名前です –

関連する問題