这可能吗?
我有一个ListView,我想为列创建一个模板,因此我标记为'cellTextBox'的每一列都显示一个文本框(并且还在LostFocus事件上调用TextBox_LostFocus())。我真的想使用单个模板,而不是为每个列定义一个DockPanel和TextBox。但是,每列将绑定到数据源中的不同列。
换句话说,我想为GridViewColumns创建一个“cookie cutter”,它允许我为每一个指定不同的绑定路径。
我尝试了类似这样的东西(除其他外),但它不起作用。
有什么想法吗?
<Page.Resources>
<DataTemplate x:Key="cellTextBox">
<DockPanel>
<TextBox
LostFocus="TextBox_LostFocus"
Width="100"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=DisplayMemberBinding}"
/>
</DockPanel>
</DataTemplate>
</Page.Resources>
<StackPanel>
<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Tables[0]}"
Width="Auto"
x:Name="Service_Tasks">
<ListView.View>
<GridView>
<GridViewColumn Width="120" CellTemplate="{StaticResource cellTextBox}" DisplayMemberBinding={Binding Path=Field1} Header="Field1" />
<GridViewColumn Width="120" CellTemplate="{StaticResource cellTextBox}" DisplayMemberBinding={Binding Path=Field2} Header="Field2" />
<GridViewColumn Width="120" CellTemplate="{StaticResource cellTextBox}" DisplayMemberBinding={Binding Path=Field3} Header="Field3" />
<GridViewColumn Width="120" CellTemplate="{StaticResource cellTextBox}" DisplayMemberBinding={Binding Path=FieldN} Header="FieldN" />
<!-- ... other columns ... -->
</GridView>
</ListView.View>
</ListView>
</StackPanel>
编辑 - 我想我会分享@H.B.的解决方案的Visual Basic翻译,这对我有用。也许这会帮助别人。
Imports System.Windows.Markup
Public Class TemplateBuilderExtension : Inherits StaticExtension
Dim _path As String
Shared _tagpath As String
'Private TagPath As String
Shared Property TagPath As String
Get
TagPath = _tagpath
End Get
Private Set(ByVal value As String)
_tagpath = value
End Set
End Property
Property Path As String
Get
Path = _path
End Get
Set(ByVal value As String)
_path = value
End Set
End Property
Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
TagPath = Path
Dim resourceExt = New StaticResourceExtension("TemplateBuilder_BaseTemplate")
'Dim baseTemplate As DataTemplate = DirectCast(resourceExt.ProvideValue(serviceProvider), DataTemplate)
ProvideValue = DirectCast(resourceExt.ProvideValue(serviceProvider), DataTemplate)
End Function
Public Sub New()
MyBase.new()
End Sub
Public Sub New(ByVal Path As String)
Me.Path = Path
End Sub
End Class
Public Class TemplateBuilderTagExtension : Inherits MarkupExtension
Public Sub New()
MyBase.New()
End Sub
Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
ProvideValue = New Binding(TemplateBuilderExtension.TagPath)
End Function
End Class
显然,无论如何,XAML都是一样的。
不要忘记将以下命名空间引用添加到根级别标记:
xmlns:me="clr-namespace:WpfApplication1"
,将WpfApplication
替换为应用程序的根命名空间(可以在应用程序的属性中找到)。
再次感谢@ H.B。!
答案 0 :(得分:4)
我现在可以想到的唯一方法就是在尝试将多个值传递给一个属性时使用markup-extensions。
将变量数据放入模板似乎并非易事,而使用静态属性的以下方法非常糟糕,也许你可以想到更好的东西:
<ListView ItemsSource="{Binding Data}">
<ListView.Resources>
<!-- x:Shared="False" because otherwise the Tag extension is only evaluated the first time the resource is accessed -->
<DataTemplate x:Shared="false" x:Key="TemplateBuilder_BaseTemplate">
<TextBox Text="{me:TemplateBuilderTag}" Width="100" LostFocus="TextBox_LostFocus" />
</DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{me:TemplateBuilder Name}" />
<GridViewColumn CellTemplate="{me:TemplateBuilder Occupation}" />
</GridView>
</ListView.View>
</ListView>
public class TemplateBuilderExtension : MarkupExtension
{
public string Path { get; set; }
public TemplateBuilderExtension() { }
public TemplateBuilderExtension(string path)
{
Path = path;
}
// Here be dirty hack.
internal static string TagPath { get; private set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
TagPath = Path;
var resourceExt = new StaticResourceExtension("TemplateBuilder_BaseTemplate");
// This line causes the evaluation of the Tag as the resource is loaded.
var baseTemplate = resourceExt.ProvideValue(serviceProvider) as DataTemplate;
return baseTemplate;
}
}
public class TemplateBuilderTagExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Binding(TemplateBuilderExtension.TagPath);
}
}
我在获取资源时尝试使用自定义服务提供程序,但不幸的是它没有到达标记的ProvideValue
,这可能是获得路径的更好方法。
您当然可以在TemplateBuilderExtension
中从头开始动态创建模板,这样您就不会遇到此类问题。