问题是这样的:
我自定义了一个可以反转的两种状态的控件,资源文件如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MusicMian"
>
<Style TargetType="{x:Type local:NativeContainer}">
<Setter Property="IsTabStop" Value="False"></Setter>
<Setter Property="Focusable" Value="False"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:NativeContainer}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="ViewStates">
<VisualState Name="ContentView">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="TooltipBorder"
Storyboard.TargetProperty="Opacity" Duration="0" To="0"
/>
</Storyboard>
</VisualState>
<VisualState x:Name="TootipView">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="ContentBorder"
Storyboard.TargetProperty="Opacity" Duration="0" To="0"
/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border Name="ContentBorder">
<ContentPresenter VerticalAlignment="Stretch"
DataContext="{TemplateBinding DataContext}"
HorizontalAlignment="Stretch" Content="{TemplateBinding ContentView}"></ContentPresenter>
</Border>
<Border Name="TooltipBorder">
<ContentPresenter VerticalAlignment="Stretch"
DataContext="{TemplateBinding DataContext}"
HorizontalAlignment="Stretch" Content="{TemplateBinding TooltipView}"></ContentPresenter>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
功能代码如下:
public class NativeContainer : Control
{
static NativeContainer()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NativeContainer),new FrameworkPropertyMetadata(typeof(NativeContainer)));
IsFilpedProperty = DependencyProperty.Register("IsFilped",typeof(bool),typeof(NativeContainer),new FrameworkPropertyMetadata(false,new PropertyChangedCallback(IsFilpedChanged)));
ContentViewProperty = DependencyProperty.Register("ContentView",typeof(object),typeof(NativeContainer));
TooltipViewProperty = DependencyProperty.Register("TooltipView", typeof(object), typeof(NativeContainer));
}
private static void IsFilpedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NativeContainer NC = (NativeContainer)d;
NC.ChangedVisualState(false);
}
private void ChangedVisualState(bool p)
{
if (IsFilped)
{
VisualStateManager.GoToState(this, "ContentView", p);
}
else
{
VisualStateManager.GoToState(this, "TootipView",p);
}
UIElement CView = ContentView as UIElement;
if (CView != null)
{
if (IsFilped)
{
CView.Visibility = Visibility.Visible;
}
else
{
CView.Visibility = Visibility.Hidden;
}
}
UIElement TView = TooltipView as UIElement;
if (TView != null)
{
if (IsFilped)
{
TView.Visibility = Visibility.Hidden;
}
else
{
TView.Visibility = Visibility.Visible;
}
}
}
public static readonly DependencyProperty IsFilpedProperty;
public static readonly DependencyProperty ContentViewProperty;
public static readonly DependencyProperty TooltipViewProperty;
public bool IsFilped
{
get { return (bool)GetValue(IsFilpedProperty); }
set { SetValue(IsFilpedProperty, value); }
}
public object ContentView
{
get { return (object)GetValue(ContentViewProperty); }
set { SetValue(ContentViewProperty, value); }
}
public object TooltipView
{
get { return (object)GetValue(TooltipViewProperty); }
set { SetValue(TooltipViewProperty, value); }
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ChangedVisualState(false);
}
}
代码很简单,不做解释。
我现在遇到的问题是:我将两个这种自定义控件放到TabControl的两个TabItem中,同样的数据源绑定方法(自定义控件中的ContentView),第一个可以绑定到数据,第二个绑定不到数据。哪位大侠可以帮我解决这个问题,表示感谢。。。