Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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# 无法从WPF中的另一个元素绑定到ZIndex_C#_Wpf_Xaml - Fatal编程技术网

C# 无法从WPF中的另一个元素绑定到ZIndex

C# 无法从WPF中的另一个元素绑定到ZIndex,c#,wpf,xaml,C#,Wpf,Xaml,我想在同一.xaml文件中绑定到WPF中另一个元素的ZIndex,但它不起作用 要绑定的元素 <Border x:Name="BubbleTop" CornerRadius="5" Background="#EBF5EB" Padding="8,6" BorderBrush="LightGray" BorderThickness="1" Grid.ZIndex="3"> <ContentPre

我想在同一
.xaml
文件中绑定到WPF中另一个元素的ZIndex,但它不起作用

要绑定的元素

<Border 
    x:Name="BubbleTop" 
    CornerRadius="5" 
    Background="#EBF5EB" 
    Padding="8,6" 
    BorderBrush="LightGray" 
    BorderThickness="1" 
    Grid.ZIndex="3">
        <ContentPresenter />
</Border>

但它不起作用。有任何提示吗?

在值转换器中尝试以下操作:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
  int z = (int)value;
  if (z == 3)
    return Brushes.Red;
  else
    return Brushes.Blue;
}

您拥有的转换器可以正常工作,但绑定的
路径
是错误的。绑定到附加属性时,必须将路径放入parens中,才能正确解析路径

也就是说,我认为转换器在这里没有多大意义。您可以使用样式来解决这样一个简单的切换。这允许您在XAML中保留更多的视图逻辑

例如:

<TextBlock
    x:Name="statusText"
    Margin="..."
    FontWeight="Bold"
    Text="...">
  <TextBlock.Style>
    <p:Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Blue"/>
      <p:Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=BubbleTop, Path=(Grid.ZIndex)}" Value="3">
          <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
      </p:Style.Triggers>
    </p:Style>
  </TextBlock.Style>
</TextBlock>


(注意:您可以省略
元素的
p:
XML名称空间。我之所以包含该名称空间,是因为当XML中有一个普通
元素时,堆栈溢出代码格式化程序会混淆,无法正确格式化XML。)

尝试返回SolidColorBrush对象而不是字符串。前台的dependency属性需要一个SolidColorBrush,并且只允许xaml中的字符串,然后由框架将其转换为笔刷。我已经按照您的建议进行了更改,但它不起作用。文本颜色仍然是黑色。@Casper:根据您发布的一点点代码,您的
前景
颜色应该始终是蓝色或红色,而不是黑色。如果是黑色,则代码中还有其他错误。虽然我不喜欢在这种情况下使用转换器,但上面的答案可以满足您的特定需求,只是您的代码在路径中也有一个bug。如果不将路径放入parens,则无法绑定到附着的属性。关于我的意思,请看我对你问题的回答。@PeterDuniho很好的解释和感谢提供的额外信息,将来会派上用场。事实上,我仔细检查了一下,我意识到字符串到画笔的转换确实发生在运行时。IMHO您的转换器比返回字符串的转换器更好(如果您可以轻松地传递正确的对象,为什么要强制转换?),但代码中唯一的实际问题是绑定路径,而不是转换器。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
  int z = (int)value;
  if (z == 3)
    return Brushes.Red;
  else
    return Brushes.Blue;
}
<TextBlock
    x:Name="statusText"
    Margin="..."
    FontWeight="Bold"
    Text="...">
  <TextBlock.Style>
    <p:Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Blue"/>
      <p:Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=BubbleTop, Path=(Grid.ZIndex)}" Value="3">
          <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
      </p:Style.Triggers>
    </p:Style>
  </TextBlock.Style>
</TextBlock>