levitali / CompiledBindings

MIT License
279 stars 14 forks source link

[Feature] Add Support for x:Bind in Styles in WPF #7

Open Insire opened 2 years ago

Insire commented 2 years ago

While improving binding performance for regular controls is nice, the real use case for me would be, to use compiled bindings in styles with datatriggers, so that i can update the UI on state changes.

Example:

<DataTemplate DataType="{x:Type local:DataEntryViewModel}">
    <Rectangle Width="10"
                Height="10"
                Margin="1">
        <Rectangle.Style>
            <Style TargetType="{x:Type Rectangle}">
                <Setter Property="Fill" Value="Gray" />
                <Setter Property="ToolTip" Value="Not Loaded" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsLoaded}" Value="True">
                        <Setter Property="Fill" Value="Green" />
                        <Setter Property="ToolTip" Value="Loaded" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsBusy}" Value="True">
                        <Setter Property="Fill" Value="Red" />
                        <Setter Property="ToolTip" Value="Busy" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter Property="Fill" Value="Blue" />
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <ToolTip>
                                    <TextBlock>
                                        <Run Text="{Binding IsLoaded, StringFormat={}IsLoaded: {0}, Mode=OneWay}" />
                                        <LineBreak />
                                        <Run Text="{Binding IsBusy, StringFormat={}IsBusy: {0}, Mode=OneWay}" />
                                        <LineBreak />
                                        <Run Text="{Binding IsActive, StringFormat={}IsActive: {0}, Mode=OneWay}" />
                                    </TextBlock>
                                </ToolTip>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
</DataTemplate>

I dont think this is currently supported. Feel free to correct me otherwise.

grafik

levitali commented 2 years ago

Technically it's no so easy to implement. And I don't think that you get much better performance here.

The DataTrigger.Binding property expects an object of BindingBase class.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.datatrigger.binding?view=windowsdesktop-6.0

So this way or other, there must be a Binding object created.

Insire commented 2 years ago

Would it then make sense to throw out the default DataTrigger and write your own, faster variant?