WPF如何评估属性以进行绑定

时间:2015-05-18 13:09:52

标签: c# wpf binding datatemplate

我正在为我们公司做一个自定义控件,我想将元素的DataTemplate定义为ResourceDictionary,以获得更多的通用性和皮肤处理。 我的控件有一个ItemsSource属性,包含所有集合。我还在我的控件中有一个DependencyProperty,它特定于要绑定的当前Item的属性名称。

一些代码:

<DataTemplate x:Key="VEGA_TokenTemplate">
  <Border x:Name="Bd" BorderBrush="{StaticResource VEGA_TokenBorderBrush}" BorderThickness="1" Background="{StaticResource VEGA_TokenBackgroundBrush}" Padding="1" Margin="1,5" HorizontalAlignment="Stretch" SnapsToDevicePixels="True">
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="20" />
      </Grid.ColumnDefinitions>
      <TextBlock Text="{Binding WHAT_HERE}" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
      <Button Background="Transparent" Content="X" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="10" FontFamily="Berlin Sans FB" Grid.Column="1" Command="{Binding RelativeSource={RelativeSource AncestorType=local:TokenTextBox}, Path=TokenDeleteButtonCommand}"
      CommandParameter="{Binding WHAT_HERE}" IsEnabled="True" />
    </Grid>
  </Border>
</DataTemplate>

在这个DataTemplate中,我想通过评估我的依赖属性来替换WHAT_HERE标记。 例如,如果我在我的依赖项属性上设置“电子邮件”,我希望绑定类似于“路径=电子邮件”。但是,我只有“电子邮件”作为我的组件的litteral。我怎么能做这样的绑定?

我希望我的解释清楚......

谢谢

1 个答案:

答案 0 :(得分:0)

我会在这种情况下使用行为或附加属性。我相信你还能有其他方法来实现这个目标。但这是一种给你一个想法的方法

//test interface and test class
public interface IProvidePropertyToBindTo
{
    string GetPropertyToBindTo();
}

public class TestChoosingPropertyToBind : IProvidePropertyToBindTo, INotifyPropertyChanged
{
    #region Fields
    public event PropertyChangedEventHandler PropertyChanged;
    private string _emailAddress;
    private string _name;
    private string _propertyName;
    #endregion Fields
    #region Properties
    public string EmailAddress
    {
        get { return _emailAddress; }
        set
        {
            if (_emailAddress == value) 
                return;
            _emailAddress = value;
            OnPropertyChanged();
        }
    }
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value)
                return;
            _name = value;
            OnPropertyChanged();
        }
    }

    public string PropertyToBindTo
    {
        set { SetPropertToBindTo(value); }
    }

    #endregion 

    #region Methods
    public string GetPropertyToBindTo()
    {
        return _propertyName;
    }

    public void SetPropertToBindTo(string propertyName)
    {
        var prop = GetType().GetProperty(propertyName);
        if (prop == null)
            throw new Exception("Property : "+propertyName+" does not exist in this object {"+this.ToString()+"}.");
        _propertyName = propertyName;
    }



    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion Methods
}

测试数据

public ObservableCollection<TestChoosingPropertyToBind> Test
    {
        get
        {
            return new ObservableCollection<TestChoosingPropertyToBind>(
                new List<TestChoosingPropertyToBind>()
                {
                    new TestChoosingPropertyToBind(){EmailAddress = "Test@test.com", PropertyToBindTo = "EmailAddress"},
                    new TestChoosingPropertyToBind(){Name = "Test", PropertyToBindTo = "Name"}
                }
                );
        }
    }

带有自定义行为的已编辑的数据模板片段

<DataTemplate x:Key="VEGA_TokenTemplate">
        <Border x:Name="Bd" BorderBrush="Red" BorderThickness="1" Background="White" Padding="1" Margin="1,5" HorizontalAlignment="Stretch" SnapsToDevicePixels="True">
            <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding WHAT_HERE}" HorizontalAlignment="Stretch" VerticalAlignment="Center" >
                    <i:Interaction.Behaviors>
                        <BehaviorLocationNamespace:MyCustomBehavior></BehaviorLocationNamespace:MyCustomBehavior>
                    </i:Interaction.Behaviors>
                </TextBlock>
                <Button Background="Transparent" 
                        Content="X" 
                        VerticalAlignment="Center" 
                        HorizontalAlignment="Center" 
                        FontSize="10" FontFamily="Berlin Sans FB" Grid.Column="1"  IsEnabled="True" />
            </Grid>
        </Border>
    </DataTemplate>

//Items Control usage
<ItemsControl ItemTemplate="{StaticResource VEGA_TokenTemplate}" ItemsSource="{Binding Test}">