向控件中的转换器提供控件的依赖属性?

发布于 2024-09-02 03:58:23 字数 2620 浏览 1 评论 0原文

我有两个对象,文化和翻译,都不复杂。 Culture 就像一个简单的 CultureInfo,但有一个额外的字段。文化可能不会经常改变,而且一开始就不会有太多改变。但是,会有很多 Translation,并且每个 Translation 都有一个 CultureID 属性。每个翻译都有一个 TranslationID,以便您可以将翻译链接在一起。数据看起来像这样:

TranslationID、CultureID、Text;
1、en,你好;
1、es,Hola;
2、en,再见;
2、es,再见;

我正在尝试构建一个控件,可以在其中绑定文化列表以及翻译列表。对于每个 TranslationID,可能并不存在适合每种文化的翻译。所以我可能有英语、西班牙语、德语和中文作为我的文化,但对于“你好吗”,我可能只有英语和德语翻译。

具有相同 TranslationID 的翻译列表将绑定到该控件。在控件内部,每种文化只有一个或零个翻译。我有一个扩展器元素的 xaml 列表框。每个 Expander 都应该有一个 Culture,并以 Culture.Name 属性作为标题。每个 Expander 的内容是一个文本框,其中包含该文化的翻译(如果翻译存在)。如果不存在,则文本框为空。到目前为止,我已经做了这么多工作。

在控件外部,可以选择一种文化来编辑翻译,即使在控件内部可以编辑任何文化翻译。如果发生这种情况就好了。当前编辑文化应该做的是使我的控件中包含该文化的扩展器成为打开控件时唯一扩展的扩展器(控件将位于弹出窗口中)。控件中列表框中的所有其他扩展器不应展开。我在控件中创建了一个依赖属性,它采用当前编辑区域性,称为 CurrentTranslationCulture。

这就是我的问题所在。我尝试使用 DataTrigger 和自定义转换器来将文化与 CurrentTranslationCulture 进行比较。但是,由于转换器是它自己的类,因此它无法访问 CurrentTranslationCulture。我尝试将其作为 ConverterParameter 传入,但无法将其绑定到 DataTrigger 绑定中的 ConverterParamater 参数。我什至不知道这是否是我应该采取的方法。有谁知道这个问题的解决方案,或者甚至更好的解决方案?提前致谢。

代码:下面的代码不能完全工作。如果你取出 ConverterParameter,它会起作用,但我所有的 Expander 都会被关闭。在 ConverterParamerter 内部,ElementName=labelTranslationEditor 是我在尝试访问控件时提供给控件的名称。

<ListBox x:Name="listTranslations" AlternationCount="2">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type office:Culture}">
                        <Expander x:Name="cultureExpander" Header="{Binding Path=Name}" IsExpanded="False">
                            <Expander.Triggers>

                            </Expander.Triggers>
                            <TextBox x:Name="tbInsertLabelText" Style="{StaticResource popupLabelTextBox}" MinWidth="300" MaxWidth="450"
                                VerticalAlignment="Top" HorizontalAlignment="Right" SpellCheck.IsEnabled="True" TextWrapping="WrapWithOverflow" />
                        </Expander>

                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=CultureID, Converter={StaticResource expandedConverter}, ConverterParameter={Binding ElementName=labelTranslationEditor, Path=CurrentTranslationCulture}}" Value="false">
                                <Setter TargetName="cultureExpander" Property="IsExpanded" Value="false" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>

I have two objects, Culture and Translation, neither of which are complicated. Culture is like a simple CultureInfo, but with an extra field. Cultures likely won't change often, and there won't be many to begin with. However, there will be many Translations, and each Translation has a CultureID property. Translations each have a TranslationID, so that you can link Translations together. The data would look something like this:

TranslationID, CultureID, Text;
1, en, Hello;
1, es, Hola;
2, en, Bye;
2, es, Adios;

I'm trying to build a control where I can bind the list of Cultures as well as the list of Translations. For each TranslationID, there may not be a Translation for every Culture. So I may have English, Spanish, German and Chinese as my Cultures, but for "How are you", I may only have the English and German Translations.

A list of Translations of the same TranslationID will be bound to the control. Inside the control there will only be one or zero Translations per Culture. I have a xaml Listbox of Expander elements. Each Expander should have one Culture, with the Culture.Name property as the header. The content of each Expander is a TextBox containing the Translation of that Culture, if the Translation exists. If it doesn't exist, the TextBox is blank. So far, I've got this much working.

Outside of the control, one of the Cultures can be picked for editing Translations, even though inside the control any Translation of a Culture can be edited. That's fine if that happens. What the current editing Culture should do is make the Expander in my control that contains that very Culture, be the only one that is Expanded when the control is opened (the control will be in a Popup). All other Expanders in the Listbox in the control should be not expanded. I have made a dependency property in my control that takes the current editing Culture, called CurrentTranslationCulture.

This is where my problem lies. I tried using a DataTrigger and a custom converter to make the comparison of a Culture to the CurrentTranslationCulture. However, since the converter is it's own class, it doesn't have access to the CurrentTranslationCulture. I tried passing it in as a ConverterParameter, but I couldn't bind it to the ConverterParamater parameter in the Binding of the DataTrigger. I don't even know if this is the approach I should be taking. Does anyone know the solution to this, or perhaps even a better one? Thanks in advance.

Code: The below code does not work completely. If you take out the ConverterParameter, it would work, but all of my Expanders would be closed. Inside the ConverterParamerter, ElementName=labelTranslationEditor, is the name I provided to my control in an attempt to access it.

<ListBox x:Name="listTranslations" AlternationCount="2">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type office:Culture}">
                        <Expander x:Name="cultureExpander" Header="{Binding Path=Name}" IsExpanded="False">
                            <Expander.Triggers>

                            </Expander.Triggers>
                            <TextBox x:Name="tbInsertLabelText" Style="{StaticResource popupLabelTextBox}" MinWidth="300" MaxWidth="450"
                                VerticalAlignment="Top" HorizontalAlignment="Right" SpellCheck.IsEnabled="True" TextWrapping="WrapWithOverflow" />
                        </Expander>

                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=CultureID, Converter={StaticResource expandedConverter}, ConverterParameter={Binding ElementName=labelTranslationEditor, Path=CurrentTranslationCulture}}" Value="false">
                                <Setter TargetName="cultureExpander" Property="IsExpanded" Value="false" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>

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

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

发布评论

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

评论(1

久光 2024-09-09 03:58:23

来自 MSDN 论坛... Expander 问题的解决方案是使用 MultiBinding 来捕获绑定到扩展器的 Culture 以及 CurrentTranslationCulture。然后将两者传递到应用适当逻辑的转换器。

http://social. msdn.microsoft.com/Forums/en-US/wpf/thread/2699e07e-5eff-4499-8aec-34f6e1e298a0

这是唯一更改的代码:

<DataTemplate.Triggers>
                            <DataTrigger Value="True">
                                <DataTrigger.Binding>
                                    <MultiBinding Converter="{StaticResource expandedConverter}">
                                        <Binding Path="CultureID" />
                                        <Binding ElementName="labelTranslationEditor" Path="CurrentTranslationCulture" />
                                    </MultiBinding>
                                </DataTrigger.Binding>
                                <Setter TargetName="cultureExpander" Property="IsExpanded" Value="True" />
                            </DataTrigger>
                        </DataTemplate.Triggers>

From the MSDN forum... The solution to the Expander problem was to use a MultiBinding to capture both the Culture that is bound to the expander, as well as the CurrentTranslationCulture. The two are then passed into the Converter which applies the appropriate logic.

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2699e07e-5eff-4499-8aec-34f6e1e298a0

Here is the only code that changed:

<DataTemplate.Triggers>
                            <DataTrigger Value="True">
                                <DataTrigger.Binding>
                                    <MultiBinding Converter="{StaticResource expandedConverter}">
                                        <Binding Path="CultureID" />
                                        <Binding ElementName="labelTranslationEditor" Path="CurrentTranslationCulture" />
                                    </MultiBinding>
                                </DataTrigger.Binding>
                                <Setter TargetName="cultureExpander" Property="IsExpanded" Value="True" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文