-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
@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 |
@stevenbrix A Default converter where for example you call the Typeconverter linked to the target type. |
@trodent83, What are you proposing the converted object's default type and representation to be? Can you achieve what you are describing using Does StringFormatConverter from |
@vatsan-madhavan String format and StringFormaterConverter presume I want to use it for string, and that is quite often the case. |
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? |
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 |
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). Is it perfect? No but it is better then having a flying exception during runtime. |
@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) |
@weltkante But as I mentioned in the beginning it was only a suggession. Anything is better then what we have now. |
I would like to second this request and suggest expected behavior: return array or list of values provided by the nested bindings. |
Currently there is no Default converter for the multibinding, make it nessasary to create one every time.
This is often quite repetative.
The text was updated successfully, but these errors were encountered: