2017-06-01 51 views
0

所以我想學習如何動態地將樣式更改應用於控件。我一直無法得到一個用戶控件來改變它的邊界刷和背景基於主窗口中的單選按鈕和usercontrol的文本屬性。基於它的usercontrol的文本屬性似乎工作。所以看起來我在做單選按鈕的isCheck屬性時出錯了。xaml usercontrol multidatatrigger從父控制和它本身(usercontrol)

我已經從原始代碼中簡化了,但是這仍然顯示了這個問題。

MainWindow.xaml

<Window x:Class="UserControlTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:UserControlTest" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <RadioButton x:Name="calcPace" TabIndex="1" Content="Pace" HorizontalAlignment="Left" Margin="34,50,0,0" VerticalAlignment="Top" GroupName="CalculationType" 
        Height="16" Width="41"/> 
     <RadioButton x:Name="calcDistance" TabIndex="2" Content="Distance" HorizontalAlignment="Left" Margin="80,50,0,0" VerticalAlignment="Top" GroupName="CalculationType" 
        Height="16" Width="61"/> 
     <RadioButton x:Name="calcTime" TabIndex="3" Content="Time" HorizontalAlignment="Left" Margin="146,50,0,0" VerticalAlignment="Top" GroupName="CalculationType" 
        Height="16" Width="42"/> 
     <local:TextBoxTime/> 
    </Grid> 
</Window> 

TextBoxTime.xaml(用戶控件):

<UserControl x:Class="UserControlTest.TextBoxTime" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:UserControlTest" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <TextBox x:Name="timeString" TabIndex="4" HorizontalAlignment="Left" Height="23" Margin="68,130,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"> 
     <TextBox.Style> 
      <Style TargetType="TextBox"> 
       <Setter Property="BorderBrush" Value="PaleGreen"/> 
       <Setter Property="Background" Value="White"/> 
       <Style.Triggers> 
        <MultiDataTrigger> 
         <MultiDataTrigger.Conditions> 
          <Condition Binding="{Binding ElementName=calcTime, Path=IsChecked}" Value="False"/> 
          <Condition Binding="{Binding ElementName=timeString, Path=Text}" Value=""/> 
         </MultiDataTrigger.Conditions> 
         <Setter Property="BorderBrush" Value="Red"/> 
         <Setter Property="Background" Value="Snow"/> 
        </MultiDataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBox.Style> 
    </TextBox> 
</Grid> 

目前我已經爲或者加入任何代碼後面。

感謝

+0

'calcTime'超出範圍。這是最好的;您不希望UserControl對父級中的同級控件的名稱有依賴關係。 –

+0

那麼在這裏獲得功能的正確方法是什麼? –

+0

將依賴項屬性添加到usercontrol。我一起拍了一個快速的例子。 –

回答

1

這裏是我怎麼可能做到這一點:

public partial class RequireableTextBox : UserControl 
{ 
    public RequireableTextBox() 
    { 
     InitializeComponent(); 
    } 

    #region IsRequired Property 
    public bool IsRequired 
    { 
     get { return (bool)GetValue(IsRequiredProperty); } 
     set { SetValue(IsRequiredProperty, value); } 
    } 

    public static readonly DependencyProperty IsRequiredProperty = 
     DependencyProperty.Register(nameof(IsRequired), typeof(bool), typeof(RequireableTextBox), 
      new PropertyMetadata(false)); 
    #endregion IsRequired Property 

    #region Text Property 
    public String Text 
    { 
     get { return (String)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = 
     DependencyProperty.Register(nameof(Text), typeof(String), typeof(RequireableTextBox), 
      // Default must be "" not null, for the trigger to understand 
      new PropertyMetadata("")); 
    #endregion Text Property 
} 

XAML

<UserControl 
    x:Class="UserControlTest.RequireableTextBox" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:UserControlTest" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300" 
    IsTabStop="False" 
    > 
    <Grid> 
     <TextBox 
      x:Name="timeString" 
      HorizontalAlignment="Left" 
      TextWrapping="Wrap" 
      VerticalAlignment="Top" 
      Width="120" 
      Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}, UpdateSourceTrigger=PropertyChanged}" 
      > 
      <TextBox.Style> 
       <Style TargetType="TextBox"> 
        <Setter Property="BorderBrush" Value="PaleGreen"/> 
        <Setter Property="Background" Value="White"/> 

        <Style.Triggers> 
         <!-- 
         Seemed right to disable when unneeded; delete this trigger 
         if you'd rather not. 
         --> 
         <DataTrigger 
          Binding="{Binding IsRequired, RelativeSource={RelativeSource AncestorType=UserControl}}" 
          Value="False" 
          > 
          <Setter Property="IsEnabled" Value="False" /> 
         </DataTrigger> 

         <MultiDataTrigger> 
          <MultiDataTrigger.Conditions> 
           <Condition 
            Binding="{Binding IsRequired, RelativeSource={RelativeSource AncestorType=UserControl}}" 
            Value="True" 
            /> 
           <Condition 
            Binding="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}" 
            Value="" 
            /> 
          </MultiDataTrigger.Conditions> 
          <Setter Property="BorderBrush" Value="Red"/> 
          <Setter Property="Background" Value="Snow"/> 
         </MultiDataTrigger> 
        </Style.Triggers> 
       </Style> 
      </TextBox.Style> 
     </TextBox> 
    </Grid> 
</UserControl> 

用法:

<StackPanel> 
    <RadioButton x:Name="calcTime" GroupName="CalculationType">Calculate Time</RadioButton> 
    <RadioButton x:Name="calcDistance" GroupName="CalculationType">Calculate Distance</RadioButton> 
    <local:RequireableTextBox 
     IsRequired="{Binding IsChecked, ElementName=calcTime}" 
     /> 
    <local:RequireableTextBox 
     x:Name="DistanceValue" 
     IsRequired="{Binding IsChecked, ElementName=calcDistance}" 
     /> 
    <!-- Just tossed this in to demonstrate the Text property --> 
    <Label Content="{Binding Text, ElementName=DistanceValue}" Foreground="Gray" /> 
</StackPanel> 
+0

謝謝埃德這工作。 –

相關問題