WPF TextBlock StringFormat绑定到父级

时间:2012-04-04 06:26:25

标签: wpf

我一直在尝试将TextBlock的Text属性的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 个答案:

答案 0 :(得分:1)

StringFormat属性只是BindingBase上的常规属性,常规属性不能只是依赖属性可以绑定目标。所以答案是:你不能这样做。

一些可能的方法:

  1. 子类TextBox并添加一个字符串格式依赖项属性,您可以绑定该属性以提供所需的功能
  2. 使用FormattedValue属性扩展您的视图模型(如果有的话)(可能有点难看)
  3. Text属性使用MultiBinding。一个绑定转到Value,一个绑定到模板化父级的ThisStringFormat。然后编写一个实现IMultiValueConverter的转换器来返回格式化的值。