如何从子元素条件中为父元素DataTrigger

时间:2016-07-27 17:36:54

标签: wpf xaml

试图找到解决以下问题的方法。

我的目标是:  如果Button中的某个子元素不包含文本,则禁用父按钮。

那么,我正在尝试做什么:

创建一个按钮:

 <Button  Style="{StaticResource ButtonStyle}" >
     <TextBlock>
         <Run Name="TxtElement1" Text=""/>
     </TextBlock>
 </Button>
 <Button  Style="{StaticResource ButtonStyle}" >
     <TextBlock>
        <Run Name="TxtElement2" Text="some text 1"/>
     </TextBlock>
 </Button>
 <Button  Style="{StaticResource ButtonStyle}" >
     <TextBlock>
        <Run Name="TxtElement3" Text="some text 2"/>
     </TextBlock>
 </Button>

现在创建一个带触发器的样式:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=TxtElement1, Path=Text}" Value="">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=TxtElement2, Path=Text}" Value="">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=TxtElement3, Path=Text}" Value="">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>
</Style>

所以,结果我得到了:所有ToggleButtons被禁用。 但是,当运行子元素中的属性Text为空时,我只需要禁用Button

也许我使用了彻底错误的方法..感谢您的关注。

2 个答案:

答案 0 :(得分:0)

我建议不要让事情变得复杂。在设置数据之前,会将按钮应用于按钮。直接将您的VMSource绑定到Button的内容属性。然后使用Loaded Event进行一些操作(在这种情况下启用/禁用。请参阅下面的剪辑。

session_start();
$_SESSION["name"] =false;
if (!isset($_SESSION["name"])) {
  $message = getname(); //can't get this to ever be called
} else {
  $message = welcome();
}

下面是你的Button_Loaded事件。

<Button  Loaded="Button_Loaded" Content="" />
<Button  Loaded="Button_Loaded" Content="some text 1" />
<Button  Loaded="Button_Loaded" Content="some text 2"/>

由于仅在更新文本后才会触发Loaded,因此您将始终看到按钮已禁用,因为没有内容。

祝你好运。

答案 1 :(得分:0)

哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇

我找到了一个非常丑陋的解决方案,可以解决你使用Run的顽固愿望。

<强> XAML

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self} , Path=Content, Converter={StaticResource Converter}, UpdateSourceTrigger=PropertyChanged}" Value="True">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<强>转换器

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return true; //No Content at all
        var block = value as TextBlock;               
        if (block != null)
        {
            block.Loaded += (sender, args) => //Inlines are only availilable if Control has loaded
            {
                var loadedBlock = sender as TextBlock;
                var inlineCount = loadedBlock.Inlines.Count == 0;
                if (!inlineCount)
                {
                    var runs = loadedBlock.Inlines.OfType<Run>();
                    foreach (var run in runs.Where(run => !string.IsNullOrEmpty(run.Text)))
                    {
                        (loadedBlock.Parent as Button).IsEnabled = true;
                    }
                }
            };
            return string.IsNullOrEmpty(block.Text);
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

<强>用法

<Button Content="Click me" Width="80" Height="20" Style="{StaticResource ButtonStyle}"/>
<Button Width="80" Height="20" Style="{StaticResource ButtonStyle}"/>
<Button Width="80" Height="20" Style="{StaticResource ButtonStyle}">
    <TextBlock Foreground="Red">
        <Run Name="TxtElement3" Text="some text 2"/>
    </TextBlock>
</Button>
<Button Width="80" Height="20" Style="{StaticResource ButtonStyle}" >
    <TextBlock>
        <Run Text=""/>
    </TextBlock>
</Button>
<Button Width="80" Height="20" Style="{StaticResource ButtonStyle}" >
    <TextBlock>
        <Run Text=""/>
        <Run Text="Some other Text"></Run>
    </TextBlock>
</Button>

<强> IMPOPRTANT

我很高兴!建议您不使用此解决方案(即使它有效)。 而是使用按钮的内容,如果你只有纯文本,只需将你的东西放在那里。

相关问题