【发布时间】:2016-11-02 15:57:56
【问题描述】:
我的问题是接收具有 1 个 XmlElement 的 DataContext,并将其传递给转换器。转换器需要完整的元素,因为它使用多个属性。问题是它接收 MS.Internal.data.xmldatacollection,其中包含一个 XmlElement,我不知道如何继续。
<UserControl x:Class="W3.Views.ComboView" ...>
<ComboBox Style="{StaticResource ComboButtonStyle}" Width="auto" Height="auto"
Text="{Binding XPath=.,
Converter={StaticResource valueFormattingConverter }}" IsEditable="True" />
</UserControl>
转换器类:
[ValueConversion(typeof(XmlElement), typeof(string))]
public class ValueFormattingConverter : IValueConverter
{
public object Convert(object val, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(string))
throw new InvalidOperationException("The target must be a List<string>");
if (val == null) return new List<string>();
XmlElement e = null;
if (val is string ) e = val as XmlElement;
if (e == null) return null;
return Information.XmlAccess.FormatXmlField(e);
}
因此,这里的 val 作为 MS.Internal.data.xmldatacollection 接收,我不知道如何处理它以获取它包含的 XmlElement。
获取数据的重要上下文: 全部的
如果我们可以修复它,那么修复也需要在这个更复杂的环境中发挥作用:
<ContentControl x:Class="W3.Views.SimpleControlChooser" >
<ContentControl.Resources>
<DataTemplate x:Key="combo" >
<W3V:ComboView />
</DataTemplate>
// skipping other templates
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource combo}" />
<Style.Triggers>
<DataTrigger Binding="{Binding XPath=@format}" Value="check">
<Setter Property="ContentTemplate" Value="{StaticResource check}" />
</DataTrigger>
<DataTrigger Binding="{Binding XPath=@format}" Value="combo">
<Setter Property="ContentTemplate" Value="{StaticResource combo}" />
</DataTrigger>
//// skipping other DataTriggers
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
对,那么数据在哪里指定?上一级:
<UserControl x:Class="W3.Views.SimpleControl" ...>
...
<W3V:ControlLabel x:Name="Label" FontSize="12"
Width="{Binding LabelWidth,
RelativeSource={RelativeSourceAncestorType=W3V:SimpleControl}}" />
<W3V:SimpleControlChooser Content="{Binding}" />
我真的不明白 Content=line。
<ItemsControl ItemsSource="{Binding XPath=*}" Margin="0,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<W3V:SimpleControl x:Name="simple" Content="{Binding}"
LabelWidth="{Binding LabelWidth,
RelativeSource={RelativeSource AncestorType=W3V:PropertyView}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这种用法目前有效,我不想破坏这个来让另一个工作。如果需要不同,我愿意在 Convert() 方法中编写条件代码以适应数据。
【问题讨论】:
标签: c# xml wpf data-binding