简体   繁体   中英

DataBinding Issue while using MultiDataTrigger

Its looks simple and i tried all possible ways i know to fix the error still no luck, looks like am missing something. Here is my code. at least the relevent part

  <ItemsControl  ItemsSource="{Binding Source}"  >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <VirtualizingStackPanel Orientation="Horizontal">
                    <ContentControl>
                        <Path x:Name="Bound" Stroke="Black">
                            <Path.Style>
                                <Style>
                                    <Style.Triggers>
                                        <MultiDataTrigger>
                                            <MultiDataTrigger.Conditions>
                                                <Condition Binding="{Binding Condition1}"
                                                           Value="true"/>
                                                <Condition Binding="{Binding Condition2}"
                                                           Value="false"/>
                                            </MultiDataTrigger.Conditions>
                                            <Setter Property="Path.Data">
                                                <Setter.Value>
                                                    <RectangleGeometry Rect="{Binding Rect1}"/>
                                                </Setter.Value>
                                            </Setter>
                                            <Setter Property="Path.Fill">
                                                <Setter.Value>
                                                    <VisualBrush>
                                                        <VisualBrush.Visual>
                                                            // Here is the Problem
                                                            <TextBlock Text="{Binding Number}"
                                                                       Width="50"
                                                                       Height="30"
                                                                       Background="White" /> 
                                                            // Binding is not working
                                                        </VisualBrush.Visual>
                                                    </VisualBrush>
                                                </Setter.Value>
                                            </Setter>
                                        </MultiDataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Path.Style>
                        </Path>
                    </ContentControl>
                </VirtualizingStackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

TextBlock in the visualBrush is not getting the value

'Number'

If i remove all the Triggers then everything work fine. somehow there is break in the binding.

That's because the VisualBrush doesn't have a DataContext. You have to use some proxy element.

  1. Define your proxy element:

     public class DataContextProxy: Freezable { public DataContextProxy() { BindingOperations.SetBinding(this, DataContextProperty, new Binding()); } public object DataContext { get { return GetValue(DataContextProperty); } set { SetValue(DataContextProperty, value); } } public static readonly DependencyProperty DataContextProperty = FrameworkElement .DataContextProperty.AddOwner(typeof (DataContextProxy)); protected override Freezable CreateInstanceCore() { return new DataContextProxy(); } } 
  2. Add it to some parent's Resources that has the DataContext you need:

     <ItemsControl ItemsSource="{Binding Source}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <VirtualizingStackPanel Orientation="Horizontal"> <ContentControl> <ContentControl.Resources> <behavior:DataContextProxy x:Key="Proxy" DataContext="{Binding}" /> </ContentControl.Resources> <Path x:Name="Bound" Stroke="Black"> ... 
  3. And then bind your TextBlock's DataContext to the proxy's DataContext:

     ... <Setter Property="Path.Fill"> <Setter.Value> <VisualBrush> <VisualBrush.Visual> <TextBlock DataContext="{Binding Source={StaticResource Proxy}, Path=DataContext}" Text="{Binding Number}" Width="50" Height="30" Background="White" /> </VisualBrush.Visual> </VisualBrush> </Setter.Value> </Setter> ... 

Haven't personally tried it, but should work... Comment if it doesn't!

Cheers.

Freezable objects likes VisualBrush is not part of the element tree(neither logical tree or VisualTree. so you have to get the datacontext and bind to Visual Property of VisualBrush.

Assuming number is from ViewModel Change your code as follows:

 <TextBlock Text="{Binding Path=Number}" Width="50"  Height="30" Background="White" />   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM