2012-10-31 2 views
0

WP7 앱이 있고 지금까지 MVVM 프레임 워크 내에서 구현했습니다.그리드의 행과 열 수 바인딩

이제이 앱을 확장하고이 부분에 그리드가 포함되며 바인딩을 통해 원하는 작업을 수행 할 수 있는지 확실하지 않습니다. 구체적으로

가변 개수의 열이 필요합니다. 바인딩으로이 작업을 수행하는 방법을 알 수 없습니다. 그리고 나서 내가 할 수있는 경우 열의 수에 따라 열 너비를 다양하게 할 수 있습니다.

가변 수와 관련된 행과 동일합니다.

여기에 필요한 모든 정보가있는 VM을 설정할 수는 있지만 작동시키기 위해 그리드에 바인딩 할 수는 없습니다. 그리드 내에 몇 가지 변수 데이터를 포함시키고 싶습니다. 바인딩으로 어떻게 할 수 있는지 알 수 없습니다. 필자는 객체 컬렉션에 바인딩 한 목록 상자로 잘 작업했지만, 이것은 상당히 다릅니다.

코드 뒤에 생성해야하는 경우입니까? 나는 그 일을 기쁘게 생각합니다 ... 그러나 가능하다면 구속력을 가지고 행복하게 시도하고 할 것입니다.

  • 감사

답변

1

당신은 현재의 그리드 컨트롤을 확장하고 일부 사용자 지정 종속성 속성 (예를 들어, 열 및 행)을 추가하고 이들에 바인딩 할 수 있습니다. 이렇게하면 MVVM 패턴을 유지할 수 있습니다.

E.G. 당신의 VM은 속성 '행'과 '열'이 있다면

public class MyGridControl : Grid 
{ 
    public static readonly DependencyProperty RowsProperty = 
    DependencyProperty.Register("Rows", typeof(int), typeof(MyGridControl), new PropertyMetadata(RowsChanged)); 

    public static readonly DependencyProperty ColumnsProperty = 
DependencyProperty.Register("Columns", typeof(int), typeof(MyGridControl), new PropertyMetadata(ColumnsChanged)); 

    public static void RowsChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
    ((MyGridControl)sender).RowsChanged(); 
    } 

    public static void ColumnsChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
    ((MyGridControl)sender).ColumnsChanged(); 
    } 

    public int Rows 
    { 
    get { return (int)GetValue(RowsProperty); } 
    set { SetValue(RowsProperty, value); } 
    } 

    public int Columns 
    { 
    get { return (int)GetValue(ColumnsProperty); } 
    set { SetValue(ColumnsProperty, value); } 
    } 

    public void RowsChanged()  
    { 
    //Do stuff with this.Rows 
    //E.G. Set the Row Definitions and heights 
    } 

    public void ColumnsChanged() 
    { 
    //Do stuff with this.Columns 
    //E.G. Set the Column definitions and widths 
    } 

의 XAML은 다음과 같이 보일 것이다 :

<local:MyGridControl 
    Rows="{Binding Rows}" 
    Columns="{Binding Columns}"> 
</local:MyGridControl> 
+0

대 - 감사합니다! – Peter