WPF XAML从模板收集属性,并将其用作另一个模板中的触发器

时间:2018-10-24 13:54:06

标签: c# wpf xaml dependency-properties controltemplate

If the button get clicked the content of the button change to "ARMED" and the textbox IsEnabled get to false and light gray.

这是我的应用程序的目标。关键是我需要资源而不是

网格中的以下工作:

  

说明:该ToggleButton具有属性 IsChecked ,通过该属性我切换了Button的内容。另外, IsChecked 在文本框中用作DataTrigger。

<Window x:Class="EnableDisableUeberButton.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:EnableDisableUeberButton"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">    
<Grid>
    <StackPanel>
            <TextBox x:Name="Software" Text="TextToEnableDisable">
                <TextBox.Style>
                    <Style TargetType="TextBox">
                        <Setter Property="IsEnabled" Value="True" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsChecked, ElementName=ToggleButton}" Value="true">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>
            <ToggleButton x:Name="ToggleButton">
                <ToggleButton.Style>
                    <Style TargetType="{x:Type ToggleButton}">
                        <Setter Property="Content" Value="Disarmed"/>
                        <Style.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Content" Value="ARMED"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ToggleButton.Style>
            </ToggleButton>
        </StackPanel>    
</Grid>

如何在资源中进行修改

  

描述:我需要作为资源的相同行为。问题是我无法绑定静态资源。当我仅使用 xaml 时有效。

代码片段资源的问题,该问题无效,我需要帮助

  

TextBox 的ControlTemplate中,我显示了所需的内容。

  • 如何从StaticResource ToggleButton 中获取属性 IsChecked
  • 如何使用 IsChecked 作为触发器来启用或禁用ToggleButton模板中文本框中的文本?

已剪切:

<Window x:Class="EnableDisableUeberButton.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:EnableDisableUeberButton"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <ControlTemplate x:Key="ToggleButton" >
        <ToggleButton >
            <ToggleButton.Style>
                <Style TargetType="{x:Type ToggleButton}">
                    <Setter Property="Content" Value="Disarmed" />
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Content" Value="ARMED" />
                            <Setter Property="Foreground" Value="Green" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>
    </ControlTemplate>
    <ControlTemplate x:Key="TextBox" x:Name="Software">
        <TextBox Text="TextToEnableDisable">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ToggleButton}">
                                <Trigger Property="{Binding Source=ToggleButton, Path=IsChecked}" Value="false">
                                    <Setter Property="TextBox.IsEnabled" Value="False" />
                                </Trigger>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TextBox.Style>
        </TextBox>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <StackPanel>       
        <Label>Template Test</Label>
        <TextBox Template="{StaticResource TextBox}" Text="TextToEnableDisable"></TextBox>
        <ToggleButton Template="{StaticResource ToggleButton}"></ToggleButton>
    </StackPanel>
</Grid>

0 个答案:

没有答案