如何在 Silverlight XAML 中引用另一个命名空间中的绑定转换器?

发布于 2024-09-01 21:16:28 字数 664 浏览 5 评论 0原文

由于您显然无法在 C# 中创建 Silverlight DataTemplate,因此我尝试在 XAML 中创建一个。我有一个需要引用的转换器,我已在另一个命名空间中用 C# 定义了该转换器。我尝试这样做:

<UserControl.Resources>
        <DataTemplate x:Key="PriceTemplate">
            <TextBlock Text="{Binding Price, Converter={Converters:PriceConverter}}" />
        </DataTemplate>
    </UserControl.Resources>

其中 Converters 是指向正确名称空间的 xmlns。但是,我收到一个编译错误:

类型“转换器:PriceConverter”是 像标记扩展一样使用,但确实 不是从 MarkupExtension 派生的。

我尝试将 System.Windows.Markup.MarkupExtension 作为父级添加到我的转换器中,但它显然在 Silverlight 中不存在。

如何在 XAML 中引用我的转换器,而不必在 XAML 中重写它?

Since you apparently can't create a Silverlight DataTemplate in C#, I'm trying to create one in XAML. I have a converter that I need to refer to, that I have defined in C# in another namespace. I've tried doing this:

<UserControl.Resources>
        <DataTemplate x:Key="PriceTemplate">
            <TextBlock Text="{Binding Price, Converter={Converters:PriceConverter}}" />
        </DataTemplate>
    </UserControl.Resources>

Where Converters is an xmlns that points to the correct namespace. However, I get a compilation error that says:

Type 'Converters:PriceConverter' is
used like a markup extension but does
not derive from MarkupExtension.

I tried adding System.Windows.Markup.MarkupExtension as a parent to my converter, but it apparently doesn't exist in Silverlight.

How can I refer to my converter in XAML, without having to rewrite it in XAML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱影子 2024-09-08 21:16:28

您想先创建一个静态资源,然后绑定到作为静态资源的转换器。

 <UserControl.Resources> 
   <conv:IntConverter x:Key="IntConverter"></conv:IntConverter> 
 </UserControl.Resources> 
 <StackPanel> 
    <TextBlock x:Name="Result" Margin="15" FontSize="20" 
              HorizontalAlignment="Center" VerticalAlignment="Center" 
               Text="{Binding Converter={StaticResource IntConverter}}"> 
    </TextBlock> 
 </StackPanel> 
</Window>

因此,“conv:”xml 命名空间在文档顶部注册,就像对自定义控件所做的那样:

xmlns:conv="clr-namespace:MyFooCompany.Converters"

此示例改编自以下链接的教程,该教程涉及 WPF 的同一问题

: dev102.com/2008/07/17/wpf-binding-converter-best-practices/" rel="noreferrer">http://www.dev102.com/2008/07/17/wpf-binding-converter-best -实践/

You want to make a static resource first, then bind to the converter that is a static resource.

 <UserControl.Resources> 
   <conv:IntConverter x:Key="IntConverter"></conv:IntConverter> 
 </UserControl.Resources> 
 <StackPanel> 
    <TextBlock x:Name="Result" Margin="15" FontSize="20" 
              HorizontalAlignment="Center" VerticalAlignment="Center" 
               Text="{Binding Converter={StaticResource IntConverter}}"> 
    </TextBlock> 
 </StackPanel> 
</Window>

So the "conv:" xml namespace was registered at the top of the document like you do with custom controls:

xmlns:conv="clr-namespace:MyFooCompany.Converters"

This example is adapted from the below linked tutorial regarding the same issue for WPF:

http://www.dev102.com/2008/07/17/wpf-binding-converter-best-practices/

自由如风 2024-09-08 21:16:28

您似乎将类型与实例混淆了。转换器类型将存在于命名空间“中”,但是在绑定中我们不指定类型作为转换器。相反,我们为绑定提供该类型的实际实例。

通常,IValueConverter 实例是无状态的,因此我们可以在加载 DataTemplate 实例的可用资源字典链中的任何位置保存一个公共实例。

在 xaml 中,我们可以通过创建新别名来覆盖另一个名称空间来引用它。考虑到这一点,您的 xaml 可能看起来像这样:-

<UserControl x:Class="SilverlightApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication1"
    xmlns:localConverters="clr-namespace:SilverlightApplication1.Converters">
    <UserControl.Resources>
        <localConverters:PriceConverter x:Key="PriceConverter" />
        <DataTemplate x:Key="Test">
            <TextBlock Text="{Binding Price, Converter={StaticResource PriceConverter}}" />
        </DataTemplate>
    </UserControl.Resources>

You seem to be confusing Types with Instances. A converter type will exist "in" a namespace however in binding we do not specify a type as the converter. Instead we give the binding an actual instance of that type.

Generally IValueConverter instances are stateless, hence we can hold a common instance anywhere in a the chain of resource dictionaries available where the instance of a DataTemplate is loaded.

In xaml we can reference another namespace by creating a new alias to cover it. With that in mind your xaml could look something like this:-

<UserControl x:Class="SilverlightApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication1"
    xmlns:localConverters="clr-namespace:SilverlightApplication1.Converters">
    <UserControl.Resources>
        <localConverters:PriceConverter x:Key="PriceConverter" />
        <DataTemplate x:Key="Test">
            <TextBlock Text="{Binding Price, Converter={StaticResource PriceConverter}}" />
        </DataTemplate>
    </UserControl.Resources>
一梦等七年七年为一梦 2024-09-08 21:16:28
<RadioButton GroupName="Group1">
    <RadioButton.Template>
        <ControlTemplate>
            <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
                <ToggleButton.Content>
                    <SymbolIcon Symbol="Edit"/>
                </ToggleButton.Content>
                <ToolTipService.ToolTip>
                    <ToolTip Content="Sample Tooltip" Placement="Mouse" />
                </ToolTipService.ToolTip>
            </ToggleButton>
        </ControlTemplate>
    </RadioButton.Template>
</RadioButton>

添加到@Rokk 发布的答案。

<RadioButton GroupName="Group1">
    <RadioButton.Template>
        <ControlTemplate>
            <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
                <ToggleButton.Content>
                    <SymbolIcon Symbol="Edit"/>
                </ToggleButton.Content>
                <ToolTipService.ToolTip>
                    <ToolTip Content="Sample Tooltip" Placement="Mouse" />
                </ToolTipService.ToolTip>
            </ToggleButton>
        </ControlTemplate>
    </RadioButton.Template>
</RadioButton>

Adding to the answer posted by @Rokk.

┾廆蒐ゝ 2024-09-08 21:16:28

除了建议在资源字典中创建 ValueConverter 实例的其他答案之外,另一个解决方案是通过 MarkupExtention 实现它:

public class IntToLetterConverter : IMarkupExtension<IValueConverter>, IValueConverter
{
    public IValueConverter ProvideValue(IServiceProvider serviceProvider)
        => (IValueConverter)this;

    object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
        => ((IMarkupExtension<IValueConverter>)this).ProvideValue(serviceProvider);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => (char)('a' + (int)value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => (int)((char)value - 'a');
}

使用此解决方案,您可以直接引用转换器(本质上是 MarkupExtension),而无需定义资源。

In addition to the other answers suggesting creating an instance of the ValueConverter in a resource dictionary, another solution is to implement it via a MarkupExtention:

public class IntToLetterConverter : IMarkupExtension<IValueConverter>, IValueConverter
{
    public IValueConverter ProvideValue(IServiceProvider serviceProvider)
        => (IValueConverter)this;

    object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
        => ((IMarkupExtension<IValueConverter>)this).ProvideValue(serviceProvider);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        => (char)('a' + (int)value);

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        => (int)((char)value - 'a');
}

With this solution you can directly reference the converter (which is in essence a MarkupExtension) whithout defining a resource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文