如何通过String将属性绑定为Propertyname

时间:2012-12-10 13:52:30

标签: wpf xaml binding properties imultivalueconverter

您好我想绑定到Xaml中的“未知”(我只获得一个字符串)属性

起初我写了一个IValueConverter但你无法绑定到ConverterParameter 所以我把它重写为IMultiValueConverter,但现在我无法弄清楚如何使用<Binding />与路径 或者我错了?

如果你写<TextBlock Text="{Binding}" />,你将获得对象Person 并使用{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=View.Columns[0].Header}}我可以访问第一行的标题文本

现在我只需要将两者结合起来,并且将获得财产权利吗?

我的测试Xaml代码:

<UserControl x:Class="Frameworktest.View.auswahl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:Frameworktest">
    <UserControl.Resources>
        <local:toPropertyConverter x:Key="PropertyConverter"/>
    </UserControl.Resources>
    <StackPanel>
        <!--...-->
        <Border BorderThickness="5" HorizontalAlignment="Left" VerticalAlignment="Top" 
                BorderBrush="Green" CornerRadius="5">
            <ListView  Name="listView1"  IsSynchronizedWithCurrentItem="False" 
                       ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged}" <!--ObservableCollection<Person>-->
                       SelectedItem="{Binding selectedItem, UpdateSourceTrigger=PropertyChanged}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Margin="1">
                                        <TextBlock.Text>
                                            <MultiBinding Converter="{StaticResource PropertyConverter}">
                                                <Binding /><!-- How do i {Binding} here?-->
                                                <Binding Source="{Binding RelativeSource={Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=View.Columns[0].Header}}" />
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                        <GridViewColumn Header="Firstname" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Margin="1" Text="{Binding Path=Name}" Width="100"/><!--works-->
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>

                        </GridViewColumn>
                        <GridViewColumn Header="Age">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Margin="1" Text="{Binding Age}" Width="50"/><!--works-->
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                   </GridView>
                </ListView.View>
            </ListView>
        </Border>
    </StackPanel>
</UserControl>

转换器:

public class toPropertyConverter : IMultiValueConverter 
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values[0].GetType().GetProperty((string)values[1]);
    }

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

模特

public class Person : MBase, IContains
{
private string _name;

public string  Name
{
    get { return _name; }
    set { _name = value;
    RaisePropertyChanged(() => Reg(() => Name));
    }
}

private string _firstname;
public string  Firstname
{
    get { return _firstname; }
    set
    {
        _firstname = value;
        RaisePropertyChanged(() => Reg(() => Firstname));
    }
}

private int _age;

public int Age
{
    get { return _age; }
    set
    {
        _age = value;
        RaisePropertyChanged(() => Reg(() => Age));
    }
}

public bool Contains(string text)
{
    string pers = string.Format("{0} {1}", Firstname, Name);

    return pers.Contains(text);

}

}

更新我目前的Multibindung

<MultiBinding Converter="{StaticResource PropertyConverter}">
       <Binding Path="."/>
       <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}" Path="View.Columns[0].Header}}" /> <!-- doesn't contain the word "Name" like i suspected -->
</MultiBinding>

最后更新

这是一个死胡同在我的情况下你不能从GridViewColumn.CellTemplate绑定到特定的列标题值

1 个答案:

答案 0 :(得分:1)

{Binding}含义意味着:{Binding Path =。}。 所以你可以使用

<Binding Path="."/>