简体   繁体   English

使用MultiDataTrigger时出现数据绑定问题

[英]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 visualBrush中的TextBlock没有获得值

'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. 那是因为VisualBrush没有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: 将其添加到具有您需要的DataContext的某些父级资源中:

     <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: 然后将您的TextBlock的DataContext绑定到代理的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. 像VisualBrush这样的可冻结对象不是元素树的一部分(逻辑树或VisualTree都不是。)因此,您必须获取数据上下文并绑定到VisualBrush的Visual属性。

Assuming number is from ViewModel Change your code as follows: 假设数字来自ViewModel,请如下更改代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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