日期:2014-05-17  浏览次数:20827 次

WPF,这个双向的MultiBinding绑定,反向为什么不见效

<Window x:Class="WPF熊俊.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        xmlns:local="clr-namespace:WPF熊俊" 
        Height="550" Width="600" Name="ddd">
    <Window.Resources>
        <local:Number Number1="8" Number2="2" x:Key="number" />
        <local:NumberConverter x:Key="转换器1" />
    </Window.Resources>
    <Grid HorizontalAlignment="Left" Height="405" VerticalAlignment="Top" Width="533">
        <TextBlock Name="textblock1" DataContext="{StaticResource number}">
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource 转换器1}"  ConverterParameter="减一下" Mode="TwoWay">
                    <Binding Path="Number1"/>
                    <Binding Path="Number2"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
        <Button Content="Button" HorizontalAlignment="Left" Height="58" Margin="161,423,0,-76" VerticalAlignment="Top" Width="68" Click="Button_Click"/>
    </Grid>
</Window>



private void Button_Click(object sender, RoutedEventArgs e)
{
    textblock1.Text = "张三";
    Number m = this.FindResource("number") as Number;
    MessageBox.Show(m.Number1.ToString());
}


class Number
{
    public int Number1 { get; set; }
    public int Number2 { get; set; }
}


class NumberConverter : IMultiValueConverter  
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)  //返回值即作为目标属性值
    {
        string number;
        switch ((string)parameter)
        {
            case "乘一下":
                number = ((Int32)values[0] * (Int32)values[1]).ToString(); //相乘