简体   繁体   中英

MultiBinding not working in DataGridTextColumn

I am trying to use a MultiBinding to update a DataGridTextColumn.

<Window x:Class="WPFBench.MultiBindingProblem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFBench"
    mc:Ignorable="d"
    Title="MultiBindingProblem" Height="300" Width="300">
<Window.Resources>
    <local:MultiValueConverter x:Key="MultiValueConverter"/>
</Window.Resources>
<Grid>
    <DataGrid x:Name="dataGrid" AutoGenerateColumns="False"
                  ItemsSource="{Binding TableA}" ColumnWidth="100*">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
            <DataGridTextColumn Header="MultiValue">
                <DataGridTextColumn.Binding>
                    <MultiBinding Converter="{StaticResource MultiValueConverter}">
                        <Binding Path="Value"/>
                        <Binding Path="Value"/>
                    </MultiBinding>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

</Grid>

Here is the converter...

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

namespace WPFBench
{
   public class MultiValueConverter : IMultiValueConverter
   {
       public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
       {
           return values[0];
       }

       public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
       {
          throw new NotImplementedException();
       }
   }
}

The code behind...

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFBench
{
/// <summary>
/// Interaction logic for MultiBindingProblem.xaml
/// </summary>
public partial class MultiBindingProblem : Window
{
    DataTable _tableA = new DataTable();
    public MultiBindingProblem()
    {
        InitializeComponent();
        _tableA.Columns.Add(new DataColumn("Value", typeof(double)));
        _tableA.Rows.Add(0.0);
        _tableA.Rows.Add(1.0);
        _tableA.Rows.Add(2.0);
        _tableA.Rows.Add(3.0);
        _tableA.Rows.Add(4.0);
        DataContext = this;
    }
    public DataTable TableA
    {
        get { return _tableA; }
    }
}
}

The single binding column is updated. The multivalue converter is called. The multivalue column remains blank. What am I doing wrong?

Your example should work, as long as you convert values[0] to its string representation in the Convert method of IMultiValueConverter (As explained by other answers here). However, your example here is a little strange, because there is no need for a MultiBinding . (I know you are aware of them, since the first column demonstrates a more proper approach).

Anyway, I think you need a MultiBinding for the Binding property of a DataGridTextColumn , when you want to set the Binding dynamically. In this case, you should send the DataContext and the path string, and retrieve its value in a IMultiValueConverter . There is an example here , similar to this situation, in which the Binding changes based on the value in the header of the DataGridTextColumn .

Hope it helps.

Assuming that your Value is a value type like an int and not a string , this issue happens because there is no implicit conversion from object to string .

Try returning a

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    return String.Format("{0}", values[0]);  // don't return an object values[0];
}

from the Convert method.

Also put a breakpoint and check if values is correctly filled as expected.

Simple demo to prove that this is the correct answer.

Start you window in the question from a button of the main window

private void button_Click(object sender, RoutedEventArgs e)
{
    MultiBindingProblem probl = new MultiBindingProblem();
    probl.DataContext = new DemoRoughViewModel();
    probl.Show();
}

using a simple, rough view model

public class DemoRoughTable
{
    public int Value { get; set; }  // Notice that it's not a string
}
public class DemoRoughViewModel
{
    public List<DemoRoughTable> TableA { get; set; } = new List<DemoRoughTable>()
    {
        new DemoRoughTable() { Value = 1 }, new DemoRoughTable() { Value = 2 }
    };
}

HTH

What am I doing wrong?

The Binding property of a DataGridTextColumn is supposed to be set to a BindingBase object and not to a data bound value .

If you intend to show a value returned by the converter in the column you should use a DataGridTemplateColumn :

<DataGridTemplateColumn Header="MultiValue">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource MultiValueConverter}">
                        <Binding Path="Value"/>
                        <Binding Path="Value"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Also make sure that you are returning a string from the converter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    return values[0].ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM