C# 使用dependency属性为2个元素设置可见性true或false
我有这样的usercontrol:C# 使用dependency属性为2个元素设置可见性true或false,c#,wpf,xaml,silverlight,windows-phone-8,C#,Wpf,Xaml,Silverlight,Windows Phone 8,我有这样的usercontrol: <UserControls:Header Grid.Row="0" isVisiableEarnPoints="False"/> 这是页眉用户控件,我使用此控件来控制几个页面 <Grid> <TextBlock Grid.Column="1" Style="{StaticResource PuzzleTalkHeader}" Text="{Binding Path=LocalizedResources
<UserControls:Header Grid.Row="0" isVisiableEarnPoints="False"/>
这是页眉用户控件,我使用此控件来控制几个页面
<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,那么我希望imgCoin和tbxEarnPoints应该是可见的,如果它是false,那么这些元素将隐藏
我正在尝试这样的事情,但是我不能得到结果,你能帮我吗
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] :
然后像这样绑定此属性:
<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"/>
但它仍然不起作用 为此,要隐藏/显示的控件的int XAML绑定一个您更改的bool属性,并将其分配给每个控件,以便将接收到的布尔值转换为控件的适当属性
下面是实现自定义转换器的具体示例:
它是Silverlight,但概念是一样的
或者,如果框架提供的行为满足您的需求,则可以简单地使用它 这很简单。。但是为什么要创建dependency属性,可以直接将viewmodel属性附加到控件的Visibility 但是如果你想创建一个像IsVisibleEarnPoints这样的新属性,那么你必须创建DP 例如`
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.
}
}`
你想让我写Visibility={Binding true/false,Converter={StaticResource VisibilityConverter}}}吗?先生,我想像这样创建一个新属性:将该属性绑定到ViewModel的boolean属性,并将Converter分配给Converter accordinglyOK,我这样设置:Image x:Name=imgCoin Grid.Column=3 Height=24 Width=24 Source=/Assets/Images/Coin.png Visibility={Binding isvisiableernpoints,Converter={StaticResource VisibilityConverter}/>
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.
}
}`