比如在C# 类里面,已经定义了一个dependency property
        sealed public partial class TestItem
        {

        public static readonly DependencyProperty IsRunningProperty = DependencyProperty.Register
            ("IsRunning", typeof(bool), typeof(TestItem), new PropertyMetadata(
                new PropertyChangedCallback(MyPropertyChangedCallback)));        static void MyPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            MessageBox.Show("Value changed");
        }
        public bool IsRunning
        {
            get { return (bool)GetValue(TestItem.IsRunningProperty); }
            set { SetValue(TestItem.IsRunningProperty, value); }
        }
        public SetRunning
        {
            IsRunning = true;
        }
........}在上面的代码中,SetRunning运行正常。改变值的时候,消息窗口能弹出。但是如果在TestItem.xaml定义一个Style,并用到一个progressbar上

    xmlns:local="clr-namespace:PerfRunner"

        <Style x:Key="ProgressBarStyle" TargetType="{x:Type Control}">
            <Setter Property="Background" Value="Ivory"></Setter>
            <Style.Triggers>
                <Trigger Property="local:TestItem.IsRunning" Value="true">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </Trigger>
                <Trigger Property="local:TestItem.IsRunning" Value="false">
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

<ProgressBar  x:Name="testProgressBar" IsIndeterminate="True" Value="100" HorizontalAlignment="Right" 
                     Width="157"  Foreground="Black" Style="{StaticResource ProgressBarStyle}"/>
会发现要是在C# 代码里面无论怎么设置IsRunning的值(true,false), PropertyChangedCallback确实被调用了,但是Visibility的值还是不会改变.....:((((((各位见过的大拿给些意见。

解决方案 »

  1.   

    说实话,你的代码有点乱修改了一下
        public class TestItem : Control
        {
            public static readonly DependencyProperty IsRunningProperty = DependencyProperty.Register
                ("IsRunning", typeof(bool), typeof(TestItem), new PropertyMetadata(
                    null));
            public bool IsRunning
            {
                get { return (bool)GetValue(TestItem.IsRunningProperty); }
                set 
                { 
                    SetValue(TestItem.IsRunningProperty, value); 
                }
            }
        }
    [code=XAML]
    <Window x:Class="WpfApplication11.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication11"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <local:TestItem x:Key="TestItem"/>        <Style x:Key="ProgressBarStyle" TargetType="{x:Type ProgressBar}">
                <Setter Property="Background" Value="Ivory"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Source={StaticResource TestItem}, Path=IsRunning}" Value="True" >
                        <Setter Property="Visibility" Value="Hidden"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <Grid>
            <ProgressBar x:Name="testProgressBar" IsIndeterminate="True" Value="100" HorizontalAlignment="Right" 
                        Width="157"  Foreground="Black" Style="{StaticResource ProgressBarStyle}"/>
            <ToggleButton HorizontalAlignment="Left" 
                          IsChecked="{Binding Source={StaticResource TestItem}, Path=IsRunning, Mode=OneWayToSource}" 
                          Margin="12,119,0,120" Name="button1" Width="75">Button</ToggleButton>
        </Grid>
    </Window>
    [/code]其实你这种需求,使用INotifyPropertyChanged就可以了,另外你Trigger的写法也不太对。
    以c#代码的角度来考虑XAML,就会清晰多Hope this helps.
      

  2.   

    多谢楼上大侠,用dependency property是整个项目的要求-,-......DataTrigger确实能行,但是不明白的是在
    ProgressBarStyle的style trigger里面,为什么用缺省的property trigger就不行呢?
      

  3.   

    你Trigger的写法有问题,依赖属性也是类的一个属性。 Style TargetType="{x:Type Control}" => Trigger Property="local:TestItem.IsRunning" 。
    如果你本身TestItem 是一个控件,这个Style是作用在TargetType="{x:Type local:TestItem}" ,可以在IsRunningProperty的PropertyChangedCallBack里面INotifyPropertyChange, 这样你属性触发器就会被触发,执行动作。如若不然,那么你的控件的触发条件就是当别的对象的一个属性发生变化时,自己要更新状态,这种要用DataTrigger,指定需要监视的是哪个对象,哪个属性。