WPF如何评估一个属性进行绑定
我正在为我们公司进行自定义控制,并且我想将元素的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在我的组件。 我该如何做这样的绑定?
我希望我在我的解释中清楚...
谢谢
我会在这种情况下使用行为或附加属性。 我相信你可以做到这一点的其他变化。 但这里有一种方法可以给你一个想法
//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}">
链接地址: http://www.djcxy.com/p/44613.html