Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default converter for Multibinding #158

Open
trodent83 opened this issue Dec 12, 2018 · 10 comments
Open

Default converter for Multibinding #158

trodent83 opened this issue Dec 12, 2018 · 10 comments
Labels
API suggestion Early API idea and discussion, it is NOT ready for implementation Design Discussion Ongoing discussion about design without consensus Enhancement Requested Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@trodent83
Copy link

Currently there is no Default converter for the multibinding, make it nessasary to create one every time.
This is often quite repetative.

@stevenbrix stevenbrix added the Enhancement Requested Product code improvement that does NOT require public API changes/additions label Dec 13, 2018
@stevenbrix stevenbrix added this to the Future milestone Dec 13, 2018
@stevenbrix
Copy link
Contributor

@trodent83 are you asking that the framework provide a default multivalue converter, or supply a way for the app to specify one so you don't need to write Converter={StaticResource MyConverter} multiple times?

@trodent83
Copy link
Author

@stevenbrix A Default converter where for example you call the Typeconverter linked to the target type.

@vatsan-madhavan
Copy link
Member

@trodent83, What are you proposing the converted object's default type and representation to be?

Can you achieve what you are describing using StringFormat in MultiBinding (instead of a Converter)?

Does StringFormatConverter from Microsoft.TeamFoundation.Controls.dll seem like about what you are looking for?

@vatsan-madhavan vatsan-madhavan added Design Discussion Ongoing discussion about design without consensus API suggestion Early API idea and discussion, it is NOT ready for implementation labels Jan 12, 2019
@trodent83
Copy link
Author

@vatsan-madhavan
I would suggest using simply the TypeDescriptor.GetConverter for the target type.

String format and StringFormaterConverter presume I want to use it for string, and that is quite often the case.

@paolo20000
Copy link

paolo20000 commented Dec 3, 2019

This is definitely an issue, I made a small example to show there's an inconsistency:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:global="clr-namespace:">
    <Window.Resources>
        <global:MyMultiConverter x:Key="MyMultiConverter" />
        <global:MyConverter x:Key="MyConverter" />
    </Window.Resources>
    <StackPanel>
        <TextBox x:Name="MyTextBox" Text="#FFFF00"/>

        <TextBlock Text="Color string through Binding and ValueConverter: Default TypeConverter applied">
            <TextBlock.Background>
                <Binding Converter="{StaticResource MyConverter}" Path="Text" ElementName="MyTextBox" />
            </TextBlock.Background>
        </TextBlock>
        <TextBlock Text="Color string through MultiBinding and MultiValueConverter: No TypeConverter applied">
            <TextBlock.Background>
                <MultiBinding Converter="{StaticResource MyMultiConverter}">
                    <Binding Path="Text" ElementName="MyTextBox" />
                </MultiBinding>
            </TextBlock.Background>
        </TextBlock>
    </StackPanel>
</Window>

The converters here just return a given value as-is:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => value;
}
public class MyMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) => values[0];
}

Any workaround without changing the converter?

@weltkante
Copy link

weltkante commented Dec 3, 2019

@trodent83

I would suggest using simply the TypeDescriptor.GetConverter for the target type.

TypeConverter from System.ComponentModel does not support reducing multiple parameters into a single value. You could pass an object array with the parameters and ask the TypeConverter to ConvertFrom the object array, but almost all default converters won't be able to do anything meaningful with it.

The whole point of MultiBinding/MultiValueConverter having no default is because there exists no sensible default for converting arbitrary lists of objects into something else.

@paolo20000
I'm not sure this is the same as what the issue is talking about, if it is meant to be the same then the issue description/discussion is misleading. You are talking about the postprocessing the framework does for the converters is inconsistent, this is not related to providing a default converter, you are already providing the converters so no default converter would be used.

@trodent83
Copy link
Author

@weltkante

The point would be that instead of having to create, and set a multi value converter at every place you might have a default typeconverter implementation, which in turn can be used from everywhere in your code. (Not only on the UI).
Also it would make possible to use arrays more of less out of the box as a type specific conversion (or conversion try) is not to complicated to implement here as well.

Is it perfect? No but it is better then having a flying exception during runtime.

@weltkante
Copy link

weltkante commented Dec 3, 2019

@trodent83 I see, so you want the ability to set an application wide default multibinding converter - this wasn't clear from the discussion above. Thanks for clarifying. Yes reusing the TypeConverter infrastructure to lookup/override default multibinding converters would be an option, though I think its worth discussing if its the right option, or if WPF should provide its own global default setting.

You can't just introduce new semantics to the existing TypeConverter infrastructure without thinking about the impact on other frameworks, since TypeConverter is shared between all UI frameworks and not just WPF.

I don't have any immediate concern or argument not to do it, but its something to think more about before implementing it.

[edit] Also another point is that WPF and WinUI want to try getting closer and not further apart in the binding infrastructure (e.g. think x:Bind), so its worth thinking about an approach that works for both WPF and WinUI.

@paolo20000 you should probably create a separate issue for your problem (which is worth fixing separately from what is discussed here)

@trodent83
Copy link
Author

@weltkante
Considering that the type is known it can make type checking to it's heart content, and using an exsisting infrastructure doesn't modifies it.

But as I mentioned in the beginning it was only a suggession. Anything is better then what we have now.

@MaximShey
Copy link

I would like to second this request and suggest expected behavior: return array or list of values provided by the nested bindings.
One of the possible usages - binding of a command parameter to values of several other controls in the same window or user control.
If there is a converter assigned to multibinding it would receive an array of values as a parameter of Convert function. If there is no converter you can just try to assign this array or similar list or other object that contains all the values to the target property.
Alternatively - provide multi converter that just returns a copy of the array or some other container with all the values from it's Convert method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API suggestion Early API idea and discussion, it is NOT ready for implementation Design Discussion Ongoing discussion about design without consensus Enhancement Requested Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

6 participants