2012-04-04 17 views
1

私は、TextBlockのTextプロパティのStringFormatをテンプレート化された親にバインドしようとしています。私はStringFormatを設定しようとしているのはここWPF TextBlock StringFormat親にバインド

は次のとおりです。ここで

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:DataFlowControls"> 

    <Style TargetType="{x:Type local:DfcEditTextBox}"> 
     <Setter Property="Margin" Value="-6, 0, -6, 0" /> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:DfcEditTextBox}"> 
        <TextBlock x:Name="PART_TextBlock" 
           Padding="2, 0, 0, 0" 
           Text="{Binding Path=Value, StringFormat=ThisStringFormat, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
        </TextBlock> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</ResourceDictionary> 

は、親である:私はそれをmucks StringFormat = ThisStringFormatを含んまで

<Window x:Class="DataFlowControls.Show.DfcListView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:dfc="clr-namespace:DataFlowControls;assembly=DataFlowControls" 
     xmlns:local="clr-namespace:DataFlowControls.Show" 
     Title="DfcListView" Height="400" Width="500"> 
    <Grid> 
     <StackPanel> 
      <dfc:DfcListView Name="lvTradesCollection" ItemsSource="{Binding Path=TradesCollection}" KeyboardNavigation.DirectionalNavigation="Continue" > 
       <ListView.ItemContainerStyle> 
        <Style TargetType="ListViewItem"> 
         <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
        </Style> 
       </ListView.ItemContainerStyle> 
       <ListView.Resources> 
        <DataTemplate x:Key="Date_DataTemplate"> 
         <dfc:DfcEditTextBox Value="{Binding Path=Date, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" ThisStringFormat='{}{0:dd/MM/yyyy}' HorizontalContentAlignment="Left" IsEditable="true" /> 
        </DataTemplate> 
        <DataTemplate x:Key="Asset_DataTemplate"> 
         <dfc:DfcEditTextBox Value="{Binding Path=Asset, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Left" IsEditable="true" /> 
        </DataTemplate> 
        <DataTemplate x:Key="Lots_DataTemplate"> 
         <dfc:DfcEditTextBox Value="{Binding Path=Lots, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" ThisStringFormat='N2' HorizontalContentAlignment="Center" IsEditable="true" /> 
        </DataTemplate> 
        <DataTemplate x:Key="Price_DataTemplate"> 
         <dfc:DfcEditTextBox Value="{Binding Path=Price, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Center" IsEditable="true" /> 
        </DataTemplate> 
        <DataTemplate x:Key="IsCheap_DataTemplate"> 
         <dfc:DfcEditCheckBox Value="{Binding Path=IsCheap, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" IsEditable="true" /> 
        </DataTemplate> 
        <DataTemplate x:Key="NextTrade_DataTemplate"> 
         <dfc:DfcEditComboBox Value="{Binding Path=NextTrade, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{x:Static local:DfcListView.NextTradeTypes}" IsEditable="true" /> 
        </DataTemplate> 
       </ListView.Resources> 
       <ListView.View> 
        <dfc:DfcGridView> 
         <dfc:DfcGridViewColumn Header="Date" Width="140" CellTemplate="{StaticResource Date_DataTemplate}" /> 
         <dfc:DfcGridViewColumn Header="Asset" Width="40" CellTemplate="{StaticResource Asset_DataTemplate}" /> 
         <dfc:DfcGridViewColumn Header="Lots" Width="40" CellTemplate="{StaticResource Lots_DataTemplate}" /> 
         <dfc:DfcGridViewColumn Header="Price" Width="50" CellTemplate="{StaticResource Price_DataTemplate}" /> 
         <dfc:DfcGridViewColumn Header="IsCheap" Width="60" CellTemplate="{StaticResource IsCheap_DataTemplate}" /> 
         <dfc:DfcGridViewColumn Header="NextTrade" Width="80" CellTemplate="{StaticResource NextTrade_DataTemplate}" />                   
        </dfc:DfcGridView> 
       </ListView.View> 
      </dfc:DfcListView> 
      <Button Content="Add Row" HorizontalAlignment="Left" Margin="5,5,5,5" Click="AddRow_Click"/> 
      <Button Content="Update Row" HorizontalAlignment="Left" Margin="5,5,5,5" Click="UpdateRow_Click"/> 
     </StackPanel> 
    </Grid> 
</Window> 

すべてが正常に動作します。しかし、私は何とかStringFormatを、親で表現されたThisStringFormatプロパティに接続する必要があります。私は、テンプレート化された親に到達しようとするためにStringFormat = ThisStringFormatを変更することで実験しましたが、無駄です。

この問題を解決する方法については、

答えて

1

StringFormatプロパティはBindingBaseの通常のプロパティに過ぎず、通常のプロパティは依存関係のプロパティのみをバインドすることはできません。だから答えは:あなたはそうすることはできません。

いくつかの可能なアプローチ:

  1. サブクラスTextBox、あなたが
  2. は、あなたのビューモデルを増補必要な機能を提供するバインド先となる文字列形式の依存関係プロパティを追加します(FormattedValueプロパティを持つ(あなたは1を持っている場合)おそらく少し醜い)
  3. TextプロパティにはMultiBindingを使用してください。 1つのバインディングは、Valueに、1つはテンプレート化された親のThisStringFormatに行きます。次に、IMultiValueConverterを実装するコンバータを作成して、書式設定された値を返します。
+1

第3は、IValueConverterでMultiBindingを使用することです。 –

+0

ありがとう...とても単純ですが、そうです。私はこの時間前に考えるべきだった! – Damian

+0

これはまだうまくいきません... – Damian

関連する問題