2012-04-04 150 views
1

我一直在嘗試將TextBlock的Text屬性的StringFormat綁定到模板父級。WPF TextBlock StringFormat綁定到父

這裏就是我想要設置的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> 

這裏是父:

<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屬性。我已經嘗試更改StringFormat = ThisStringFormat以嘗試訪問模板父級,但無濟於事。

關於如何解決這個問題的任何想法?

回答

1

StringFormat屬性只是BindingBase上的常規屬性,常規屬性不能綁定目標只有依賴屬性可以。所以答案是:你不能這樣做。

一些可能的方案:

  1. 子類TextBox並添加一個字符串格式依賴屬性。它可以綁定提供所需的功能
  2. 增強您的視圖模型(如果有的話)與FormattedValue的財產(可能有點難看)
  3. 使用MultiBinding作爲Text屬性。一個綁定轉到Value,另一個轉到模板父級的ThisStringFormat。然後編寫一個實現IMultiValueConverter的轉換器以返回格式化的值。
+1

第三個是使用MultiBinding和IValueConverter。 – 2012-04-04 07:28:13

+0

非常感謝...非常簡單,但非常正確。我應該想到這個小時前! – Damian 2012-04-04 08:36:10

+0

這還不行... – Damian 2012-04-04 08:58:51