开发者

Provide a dependency property of a control to a converter in the control?

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: T开发者_JS百科he 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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜