Silverlight/WPF 自定义控件的数据绑定问题

时间:2022-11-07 16:22:32

不知道别人有没有遇到过这个问题,在SL里自定义了一个控件,然后搞了一堆依赖属性,但是在用到这个控件,并且给这些属性再绑定值的时候死活绑不上!

一直都对这个问题很无奈,每次都费劲巴拉的在cs里写binding,不过管用就行!

不说废话了,上代码:

首先是自定义的控件 AniItem  -〉public partial class AniItem : UserControl

给它搞了几个属性: ImgPath, ImgWidth, ImgHeight

 public string ImgPath
{
get { return (string)GetValue(ImgPathProperty); }
set { SetValue(ImgPathProperty, value); }
}
public static readonly DependencyProperty ImgPathProperty =
DependencyProperty.Register("ImgPath", typeof(string), typeof(AniItem), new PropertyMetadata(null));

public double ImgWidth
{
get { return (double)GetValue(ImgWidthProperty); }
set { SetValue(ImgWidthProperty, value); }
}
public static readonly DependencyProperty ImgWidthProperty =
DependencyProperty.Register("ImgWidth", typeof(double), typeof(AniItem), new PropertyMetadata(0.0));

public double ImgHeight
{
get { return (double)GetValue(ImgHeightProperty); }
set { SetValue(ImgHeightProperty, value); }
}
public static readonly DependencyProperty ImgHeightProperty =
DependencyProperty.Register("ImgHeight", typeof(double), typeof(AniItem), new PropertyMetadata(0.0));

控件的样式:

  <StackPanel HorizontalAlignment="Left" Margin="0,0,0,-100" VerticalAlignment="Bottom" RenderTransformOrigin="0,0.5" x:Name="stackPanel" Grid.Row="1">
<StackPanel.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</StackPanel.RenderTransform>
<Image Stretch="Uniform" RenderTransformOrigin="0.5,0.5" x:Name="image" Margin="0,0,0,0">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
<Image.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#00FFFFFF" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="0.018"/>
</LinearGradientBrush>
</Image.OpacityMask>
</Image>
<Image Stretch="Uniform" RenderTransformOrigin="0.5,0.5" IsHitTestVisible="False" x:Name="image1" Margin="0,0,0,0">
<Image.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#00000000" Offset="0.504"/>
<GradientStop Color="#00FFFFFF" Offset="1"/>
<GradientStop Color="#33FFFFFF" Offset="0.987"/>
</LinearGradientBrush>
</Image.OpacityMask>
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
</StackPanel>

如果我们把ImgPath,ImgWidth,ImgHeight都在这个xaml里绑定上,在用到这个AinItem的时候是不会成功的!

<ctr:AniItem ImgPath="{Binding ImageUrl}"   

             ImgHeight="{Binding ImageHeight}"  ImgWidth="{Binding ImageWidth}" />

接下来我们需要做的是把绑定部分写在代码里:

   public AniItem()
{
InitializeComponent();
this.DataContext = this;

this.SetBinding(this.image, Image.WidthProperty, "ImgWidth");
this.SetBinding(this.image, Image.HeightProperty, "ImgHeight");
this.SetBinding(this.image, Image.SourceProperty, "ImgPath");

this.SetBinding(this.image1, Image.WidthProperty, "ImgWidth");
this.SetBinding(this.image1, Image.HeightProperty, "ImgHeight");
this.SetBinding(this.image1, Image.SourceProperty, "ImgPath");

this.SetBinding(this.stackPanel, StackPanel.WidthProperty, "ImgWidth");
}

private void SetBinding(FrameworkElement obj, DependencyProperty p, string path)
{
Binding b = new Binding();
b.Source = this;
b.Path = new PropertyPath(path);
b.Mode = BindingMode.OneWay;
obj.SetBinding(p, b);
}


这样,才能让这个自定义的控件老实干活~~

 

对了, 如果自定义的控件没有用xaml+cs,而是用的cs+style,那可以把绑定的代码放到 OnApplyTemplate里头~然后那些控件的名字可以用

GetTemplateChild(name)找到