Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MultiDataTrigger时出现数据绑定问题_C#_Wpf_Xaml - Fatal编程技术网

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

C# 使用MultiDataTrigger时出现数据绑定问题,c#,wpf,xaml,C#,Wpf,Xaml,它看起来很简单,我尝试了我知道的所有可能的方法来修复错误,但仍然没有运气,看起来我遗漏了一些东西。 这是我的密码。至少是相关部分 <ItemsControl ItemsSource="{Binding Source}" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid/> </It

它看起来很简单,我尝试了我知道的所有可能的方法来修复错误,但仍然没有运气,看起来我遗漏了一些东西。 这是我的密码。至少是相关部分

  <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>

//问题就在这里
//绑定不起作用
visualBrush中的TextBlock未获取该值

“数字”


如果我移除所有触发器,那么一切都会正常工作。某种程度上,绑定出现了中断。

像VisualBrush这样的可自由化对象不属于元素树(既不是逻辑树也不是VisualTree)。因此,您必须获取datacontext并绑定到VisualBrush的Visual属性

假设数字来自ViewModel,请按如下方式更改代码:

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

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

  • 定义代理元素:

    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();
        }
    }
    
  • 将其添加到具有所需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">
                        ...
    
    
    ...
    
  • 然后将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>
    ...
    
    。。。
    ...
    
  • 没有亲自尝试过,但应该有效…如果没有,请评论

    干杯