使用依赖项属性为2个元素设置Visibility true或false

时间:2014-11-21 14:22:09

标签: c# wpf xaml silverlight windows-phone-8

我有这样的usercontrol:

它是标题用户控件,我将此控件用于少量页面,

 <Grid>
            <TextBlock Grid.Column="1" Style="{StaticResource PuzzleTalkHeader}"  Text="{Binding Path=LocalizedResources.GlobalApplicationTitle, Source={StaticResource LocalizedStrings}}" Grid.ColumnSpan="2"/>
            <Image x:Name="imgCoin" Grid.Column="3" Height="24" Width="24" Source="/Assets/Images/Coin.png" />
            <TextBlock x:Name="tbxEarnPoints" Grid.Column="5" Text="15000"/>
        </Grid>

我想设置一个这样的属性:isVisibleEarnPoints="True"

<UserControls:Header Grid.Row="0" isVisibleEarnPoints="True"/>

如果isVisibleEarnPoints="True",那么我希望imgCointbxEarnPoints应该是可见的,如果它是假的,则那些元素隐藏

我正在尝试这样的事情,但我无法得到结果,你能帮助我吗?

    public partial class Header : UserControl
    {
        public Header()
        {
            InitializeComponent();

        }

        //public bool isVisiableEarnPoints
        //{
        //  set
        //  {
        //      if(value)
        //      {
        //          imgCoin.Visibility = Visibility.Visible;
        //          tbxEarnPoints.Visibility = Visibility.Visible;
        //      }
        //      else
        //      {
        //          imgCoin.Visibility = Visibility.Collapsed;
        //          tbxEarnPoints.Visibility = Visibility.Collapsed;
        //      }
        //  }
        //}

        public int isVisiableEarnPoints { get; set; }

        public static readonly DependencyProperty DisplayTypeProperty = DependencyProperty.Register("isVisiableEarnPoints", typeof(isVisiableEarnPoints), typeof(Header), new PropertyMetadata(YourDPCallBack));

        private static void YourDPCallBack(DependencyObject instance, DependencyPropertyChangedEventArgs args)
        {
            Header control = (Header)instance;

        }
    }
}

根据评论更新了问题:

[1]:

public partial class Header : UserControl
    {
        public Header()
        {
            InitializeComponent();

        }

        private bool _isisVisiableEarnPoints;
        public bool isVisiableEarnPoints
        {
            get{

                return _isisVisiableEarnPoints;
            }
            set
            {
                _isisVisiableEarnPoints=value;
            }
        }
    }

然后像这样绑定这个属性:

<Image x:Name="imgCoin" Grid.Column="3" Height="24" Width="24" Source="/Assets/Images/Coin.png" Visibility="{Binding isVisiableEarnPoints, Converter={StaticResource VisibilityConverter}}"  />
        <TextBlock x:Name="tbxEarnPoints" Grid.Column="5" Text="15000" Visibility="{Binding isVisiableEarnPoints, Converter={StaticResource VisibilityConverter}}" />

并在usercontrol中使用,如下所示:

<UserControls:Header Grid.Row="0" isVisiableEarnPoints="False"/>

但仍然没有用!

2 个答案:

答案 0 :(得分:0)

为此目的,您要隐藏/显示的控件的int XAML绑定了您更改的bool属性也将Converter分配给它们中的每一个,为了将接收到的布尔值转换为控件的适当Visibility属性。

这里实际上是实现自定义转换器的具体示例:

Using a value converter to bind to Visibility in the Silverlight data grid (它是Silverlight,但概念是相同的)

或者,如果其行为满足您的要求,则可以使用框架提供的BooleanToVisibilityConverter

答案 1 :(得分:0)

这很简单..但是为什么要创建依赖属性,可以直接将viewmodel属性附加到您拥有的控件的可见性。

但是如果你想创建像IsVisibleEarnPoints这样的新属性,那么你必须创建DP。

对于Eg。

 public partial class YOUCLASS: UserControl
    {
        public YOUCLASS()
        {
            InitializeComponent();


        }
 public static readonly DependencyProperty IsVisibleEarnPointsProperty =
            DependencyProperty.Register("IsVisibleEarnPoints", typeof(bool), typeof(YOURCLASS), new UIPropertyMetadata((bool)false, OnStateChange));

        public bool IsVisibleEarnPoints
        {
            get
            {
                return (bool)GetValue(IsVisibleEarnPointsProperty );
            }
            set
            {
                SetValue(IsVisibleEarnPointsProperty , value);
            }
        }

        private static void OnStateChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is bool)
            {
                var source = (YOURCLASS)d;
                source.SetState((bool)e.NewValue);
            }
        }

        public void SetState(bool isDisabled)
        {   

if(isDisabled)
{
imgecoin.Visibility=Visibility.Visible;
//same for other control
}
else
{
// Do whatever you want.
}




    }`