WPF MultiBinding TextBlock.Text

时间:2012-04-04 14:52:34

标签: wpf

以下代码用于绑定ListView,但对ListView的任何更改都不会返回到内存数据结构中。当我有标准绑定时,它可以两种方式工作,但不能使用多重绑定。我需要使用multibinding来获取转换器中使用的StringFormat属性。

您能告诉我一种方法来获得转换器的'ConvertBack'方法吗?

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

    <local:ValueToFormatedValueConverter x:Key="valueToFormatedValueConverter" />

    <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">                               
                        <TextBlock.Text>
                            <MultiBinding Converter="{StaticResource valueToFormatedValueConverter}">   
                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="StringFormat" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
<!--Text="{Binding Path=Value, Mode=TwoWay, Converter={StaticResource valueToFormatedValueConverter}, RelativeSource={RelativeSource TemplatedParent}}" />-->

<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}" StringFormat='{}{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}" StringFormat='N0' HorizontalContentAlignment="Center" IsEditable="true" />
                    </DataTemplate>
                    <DataTemplate x:Key="Price_DataTemplate">
                        <dfc:DfcEditTextBox Value="{Binding Path=Price, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" StringFormat='N2' 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>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows;

namespace DataFlowControls
{
    public class ValueToFormatedValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            object valueObject = values[0];
            string stringFormat = (string)values[1];

            if (stringFormat == null)
            {
                return valueObject.ToString();
            }
            if (stringFormat.Substring(0, 1) == "N")
            {
                int intValue;
                if (Int32.TryParse(valueObject.ToString(), out intValue))
                {
                    return (intValue * 100).ToString(stringFormat);
                }
                double doubleValue;
                if (Double.TryParse(valueObject.ToString(), out doubleValue))
                {
                    return doubleValue.ToString(stringFormat);
                }
                decimal decimalValue;
                if (Decimal.TryParse(valueObject.ToString(), out decimalValue))
                {
                    return decimalValue.ToString(stringFormat);
                }
            }
            if (stringFormat == "{0:dd/MM/yyyy}")
            {
                return ((DateTime)valueObject).ToShortDateString();
            }
            throw new NotImplementedException();
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return Enumerable.Repeat(value, targetTypes.Length).ToArray();
        }      
    }
}

1 个答案:

答案 0 :(得分:3)

您需要在MultiBinding上明确设置TwoWay模式:

<TextBlock.Text> 
    <MultiBinding Converter="{StaticResource valueToFormatedValueConverter}"
                  Mode="TwoWay">    
        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/> 
        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="StringFormat" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/> 
    </MultiBinding> 
</TextBlock.Text>