首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用DataBinding时发出MultiDataTrigger问题

使用DataBinding时发出MultiDataTrigger问题
EN

Stack Overflow用户
提问于 2015-06-11 08:45:39
回答 2查看 215关注 0票数 1

它看起来很简单,我尝试了我所知道的所有可能的方法来修复这个错误--仍然没有运气,看起来好像我错过了什么。这是我的密码。至少相关的部分

代码语言:javascript
复制
  <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在visualBrush中没有得到值

“‘Number”

如果我删除所有触发器,那么一切都正常。不知何故,装订中出现了裂痕。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-11 09:44:38

这是因为VisualBrush没有DataContext。您必须使用代理元素。

  1. 定义代理元素: 公共类DataContextProxy:可冻结的{公共DataContextProxy() { BindingOperations.SetBinding(this,DataContextProperty,新绑定());}公共对象DataContext { get {返回GetValue( DataContextProperty );} set { SetValue(DataContextProperty,value);} public静态只读DependencyProperty DataContextProperty= FrameworkElement .DataContextProperty.AddOwner(typeof (DataContextProxy));}}
  2. 将其添加到具有您需要的DataContext的某些父资源中: “
  3. 然后将TextBlock的DataContext绑定到代理的DataContext: .

还没亲自试过,但应该管用.评论,如果它没有!

干杯。

票数 1
EN

Stack Overflow用户

发布于 2015-06-11 09:36:15

像VisualBrush这样的可冻结对象不是元素树的一部分(逻辑树或VisualTree )。因此,您必须获取数据文本并绑定到VisualBrush的可视属性。

假设数字来自ViewModel,那么修改代码如下:

代码语言:javascript
复制
 <TextBlock Text="{Binding Path=Number}" Width="50"  Height="30" Background="White" />   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30775850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档