2016-07-27 63 views
0

試圖找到解決以下問題的方法。如何從子元素條件的父元素DataTrigger

我的目標是: 如果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被禁用。 但我需要只禁用按鈕時,屬性文本中運行子元素爲空

也許我使用根本上錯誤的方法..感謝您的任何關注。

+0

Tbh,這完全沒有意義......爲什麼你甚至會像這樣設計你的按鈕?使用按鈕的「內容」,並進行自我約束 – lokusking

+0

在我的情況下,我使用按鈕內容的幾個來源。看起來像:' ' –

+0

好吧。對你有好處。不改變它真的很奇怪的事實,這不會工作。樣式在**按鈕之前定義**,因此對按鈕的內容沒有任何瞭解(對於一些簡單的文本,您應該真的使用它)。解決方法可能是「ControlTemplate」或「行爲」或「代碼隱藏」。但是對於普通的XAML,你會有一段糟糕的時光 – lokusking

回答

0

我建議不要讓事情變得複雜。在設置數據之前,樣式應用於按鈕。直接將VMSource綁定到Button的內容屬性。然後,由於加載的文本被更新後才發射使用Loaded事件做一些操作(在這種情況下啓用/禁用。見下面剪斷。

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

下面將是你Button_Loaded事件。

private void Button_Loaded(object sender, RoutedEventArgs e) 
{ 
    Button _button = (Button)sender; 
    if (string.IsNullOrEmpty(_button.Content.ToString())) 
     _button.IsEnabled = false; 
} 

,你總會看到按鈕被禁用,因爲沒有內容。

好運。

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

我高度評價!建議您到不使用此解決方案(即使它有效)。 取而代之的是使用按鈕的內容,如果你只有純文本,就把你的東西放在那裏。

相關問題