views:

42

answers:

2

Hello,

I have a problem that i cant solve :( I have a user control (xaml file and cs file)

in xaml it's like:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Demo.CtrlContent"
x:Name="UserControl"
d:DesignWidth="598.333" d:DesignHeight="179.133" xmlns:Demo="clr-namespace:Demo" >
<UserControl.Resources>
    <Storyboard x:Key="SBSmall">
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(FrameworkElement.Width)">
            <SplineDoubleKeyFrame KeyTime="00:00:01" Value="I WANT TO BIND VALUE HERE"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
<Border BorderBrush="#FFC2C0C1" CornerRadius="3,3,3,3" BorderThickness="1,1,1,1" RenderTransformOrigin="0.5,0.5" x:Name="border" Margin="1,3,1,3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300">

and .cs file:

public partial class CtrlContent {

    private mindef W { get { return (mindef) Window.GetWindow(this); } }
    public double MedWidth { // I WANT BIND THIS VALUE GO TO STORYBOARD VALUE IN XAML ABOVE
        get {
            double actualW;
            if(W == null) actualW = SystemParameters.PrimaryScreenWidth;
            else actualW = W.WrapMain.ActualWidth;
            return actualW - border.Margin.Left - border.Margin.Right;
        }
    }
    public double SmlWidth { get { return MedWidth / 2; } }

    public CtrlContent () { this.InitializeComponent(); }
    public CtrlContent (Content content) {
        this.InitializeComponent();
        Document = content;
    }
}

in my .cs file there's a property called MedWidth, and in XAML file there's a storyboard called: SBSmall I want to bind my storyboard value to my property in class ctrlcontent.

*the idea is, the storyboard is an animation to resize the control to a certain width depends on its parent container (the width is dynamic)

anybody? please :) thanks!

A: 

You need to implement MedWidth as a DependencyProperty.

darja
+1  A: 

In your CtrlContent constructor, assign DataContext = this;

Then in your xaml:

<SplineDoubleKeyFrame Value="{Binding Path=MedWidth}"

That's the minimum required to get the xaml to read from your MedWidth property at least once at startup. If you want the databinding to update when the MedWidth property changes value, you'll need to implement INotifyPropertyChanged on CtrlContent and send the change notifications.

Note that assigning this to the DataContext is a bit of a sloppy hack. It exposes your entire class to anything running around on the xaml side of the house. A better separation of UI from logic would be to create a separate small class that is used only for data binding, and assign that to DataContext in the CtrlContent constructor. That will also keep the INotifyPropertyChanged overhead out of your main class.

dthorpe
Hi dthorpe,thanks gfor your response.It's work, but when i play the storyboard, it's not run correctly.if i print the binded MedWidth value at textbox, it's shown the correct value, but when i play the storyboard, it's shrinking the container to 0 (or null-i'm not sure)but if i hardcoded the storyboard value the animation work properly.
AnD
Your MedWidth calculation is dependent upon widths of other laid out elements, such as the window and border. It's possible that your MedWidth property value is being read by databinding before the values of these outer controls have been fully determined. Try setting the width of the window to a fixed value instead of "Auto" and see if that changes the behavior.
dthorpe