ComboBox与多重绑定作为选定项目?

时间:2011-08-11 12:48:58

标签: wpf data-binding

我正在尝试执行以下操作:

Data Binding in Combobox

但是使用多重绑定..这意味着我想要多个绑定..

所以我可以显示,例如,fullname作为firstname + lastname。

我们怎么能这样做?

谢谢!

5 个答案:

答案 0 :(得分:8)

像这样添加ItemTemplate

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0}, {1}">
                    <Binding Path="LastName" />
                    <Binding Path="FirstName" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate>

StringFormat Reference

旁注:在实际代码中,我更经常使用Dean's method,但这是MultiBinding的处理方式。

答案 1 :(得分:4)

你的意思是像这样的简单

<ComboBox>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding FirstName}" />
                <Run Text=" " />
                <Run Text="{Binding LastName}" />
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

答案 2 :(得分:1)

您可以创建一个绑定作为MultiBinding,如下所示:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding Converter=" ... " ... >
      <Binding Path="FirstName" />
      <Binding Path="LastName" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

答案 3 :(得分:1)

像这样创建MultiBinding:

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfTestApp="clr-namespace:WpfTestApp" Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
    <WpfTestApp:ConcatenateStringsConverter x:Key="_concatenateStringsConverter" />
</Window.Resources>

<Grid x:Name="LayoutRoot" Style="{StaticResource RectangleHighlighter}">
    <ComboBox Width="200" Height="40">
        <ComboBox.Items>
            <ComboBoxItem >
                <ComboBoxItem.Content>
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding Converter="{StaticResource _concatenateStringsConverter}">
                                <Binding Mode="OneWay" Path="FirstName" />
                                <Binding Mode="OneWay" Path="LastName" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </ComboBoxItem.Content>
            </ComboBoxItem>
        </ComboBox.Items>
    </ComboBox>

</Grid>
</Window>

我使用MainWindowViewModel作为Window的DataContext:

public partial class MainWindow : Window
{
 public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
    }
}


public class MainWindowViewModel :INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
        FirstName = "Souvik";
        LastName = "Basu";
    }


    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName != value)
            {
                _firstName = value;
                OnPropertyChange("FirstName");
            }
        }
    }
    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set
        {
            if (_lastName != value)
            {
                _lastName = value;
                OnPropertyChange("LastName");
            }
        }
    }

    protected void OnPropertyChange(string propertyName)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

转换器连接多个绑定值..

class ConcatenateStringsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0].ToString() + " " + values[1].ToString();
    }

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

答案 4 :(得分:0)

这里给出的例子几乎就是你所需要的。

http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

只需将TextBlock交换为ComboBox并绑定到其SelectedItem而不是Text属性。