답변됨 DataContext

  • 2012년 7월 31일 화요일 오전 7:30
     
      코드 있음

    I have an  object property. If i set DataContext throw mark up then binding dose no work

      <Grid Name="Grid2" DataContext=" strategy"

    But if i do the sane in c# code then i have no problem and binding works well.

    Grid1.DataContext = Grid2.DataContext = strategy;
    Why? Thank you

모든 응답

  • 2012년 7월 31일 화요일 오전 8:37
     
     

    hi,

    i think you should give  in xaml the DataContext as follows

    <Grid DataContext="{Binding Path=strategy}">

    refer following link for more information

    http://www.codeproject.com/Articles/321899/DataContext-in-WPF


    Thanks & Regards dhampall

  • 2012년 7월 31일 화요일 오전 10:01
     
     
    Unfortunatly , this did not help 
  • 2012년 7월 31일 화요일 오전 10:09
     
      코드 있음

    Try this,

    <Grid x:Name="Grid1"> </Grid>

    <Grid x:Name="Grid2" DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid},Mode=FindAncestor},Path=DataContext}"></Grid>

    Code Behind:(C#)

    Grid1.DataContext = Strategy;


    • 편집됨 Srithar 2012년 8월 1일 수요일 오전 4:45
    •  
  • 2012년 7월 31일 화요일 오전 10:37
     
     

    hi,

    can you plz post your code ?


    Thanks & Regards dhampall

  • 2012년 7월 31일 화요일 오전 10:45
     
     제안된 답변 코드 있음

    I Assume the strategy property is located in your window class,

    than you need to add 

    DataContext="{Binding RelativeSource={x:Static RelativeSource.Self}}"

    to the window tag, and than add the 

    DataContext="{Binding strategy}"

    line to the grid tag.

    If the strategy isn't located in your window than you need to create an instance of the class in the resource section and than bind to it.

    see: http://msdn.microsoft.com/en-us/magazine/cc163299.aspx

    Regards

    Yoni

  • 2012년 7월 31일 화요일 오전 11:38
     
      코드 있음

    I haev strategy as member of a Window class

      public Strategy strategy=new Strategy(); 		public MainWindow()		{

    And after i do this in some method of the windows class

    strategy = new Strategy(_candleManager, security, (Portfolio)Portfolios.SelectedValue, int.Parse(periodBox.Text), LotSizeValue); Grid1.DataContext = Grid2.DataContext = Grid3.DataContext=strategy;

    And in markup i author that 

    Text = "{Binding Path=Security.LastTrade.Price}"

    And it is works perfect! But how i can do this inside XAML ?

  • 2012년 7월 31일 화요일 오후 1:03
     
     

    Hi,

    I think what you are missing is setting the Window's datacontext ion the constructor

    public MainWindow()

    {

    DataContex = this;

    }

    After adding this line your xaml will be able to bind to all public properties (in your case strategy) in your code behind.

    Hope that helps?

  • 2012년 8월 2일 목요일 오전 7:41
     
     답변됨

    Hi qpiles,

    Strategy MUST be a public property! simply change that, and the code I provided earlier should work.

    Regards

    Yoni


    • 편집됨 Yoni Horovitz 2012년 8월 2일 목요일 오전 7:42
    • 답변으로 제안됨 MDinc 2012년 8월 7일 화요일 오전 8:33
    • 답변으로 표시됨 Kee PoppyModerator 2012년 8월 9일 목요일 오후 1:08
    •  
  • 2012년 8월 7일 화요일 오전 8:09
    중재자
     
     답변됨 코드 있음

    Hi qpiles,

    How's your problem now?

    Like Yoni said, Strategy must be public. To set the DataContext in xaml, you can do this:

      xmlns:local="clr-namespace:WpfApplication1"
      <Grid.DataContext>
         <local:Strategy/>
      </Grid.DataContext>
    Have a nice day,

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us