0

GridViewを持つAllPageViewというページがあります。GridViewのデータテンプレートは次のとおりです

<GridView.ItemTemplate>
     <DataTemplate>
          <Canvas Width="{Binding TemplateWidth}" Height="{Binding TemplateHeight}">
              <Canvas.Background>
                  <ImageBrush ImageSource="{Binding PageBackground}"/>
               </Canvas.Background>
                <Image Height="{Binding TemplateHeight}" Width="{Binding TemplateWidth}" Source="{Binding Page}" Stretch="Uniform" Opacity="1" CacheMode="BitmapCache" />

                   <StackPanel x:Name="EditDeleteStackPanel" Width="{Binding TemplateWidth}" Height="{Binding TemplateHeight}" Opacity="0.95">
                       <Button x:Name="NoteDelete"  HorizontalAlignment="Right"   VerticalAlignment="Top" Foreground="{x:Null}" Tapped="NoteDelete_Tapped" MinWidth="50" MinHeight="50" Margin="0,0,10,0" BorderBrush="{x:Null}" >
                           <Button.Background>
                               <ImageBrush ImageSource="{Binding Delete}"/>
                           </Button.Background>
                       </Button>
                       <Button x:Name="NoteEdit"  HorizontalAlignment="Right"   VerticalAlignment="Top"   FontFamily="Segoe Script" FontSize="24" BorderBrush="{x:Null}" Tapped="NoteEdit_Tapped" Foreground="{x:Null}" MinWidth="50" MinHeight="50" Margin="0,0,10,0">
                           <Button.Background>
                               <ImageBrush ImageSource="{Binding Edit}"/>
                           </Button.Background>
                       </Button>
                  </StackPanel>
           </Canvas>
       </DataTemplate>
</GridView.ItemTemplate>

画像の高さと幅は、それぞれクラスTemplateHeightとにバインドされています。私はとのためのより良いセッターを持っています。TemplateWidthPage_CollectionTemplateHeightTemplateWidth

public static int TemplateWidth
{
    get { return m_templateWidth; }
    set 
    { 
        m_templateWidth = value;                
    }
}

問題は、というページから画像サイズのサイズを変更する必要があるということGeneralです。
トグルスイッチを切り替えると、画像のサイズを変更する必要があります。このような

private void OnCompactCategoryToggled(object sender, RoutedEventArgs e)
{
    if (compactCateg.IsOn == true)
    {
        Page_Collection.TemplateHeight = 100;
        Page_Collection.TemplateWidth = 350;
    }
    else
    {
        Page_Collection.TemplateHeight = 200;
        Page_Collection.TemplateWidth = 700;
    }
}

AllPageViewページはにバインドされていPage_Collectionますが、値は更新されないため、画像サイズは同じです。GeneralはのaFlyoutですSettingsPane

私はWindows8を初めて使用しますが、これが初めてDataBindingです。誰かが私がどこで間違っているのか、または私が何かを逃しているのか教えてもらえますか?

編集

これはの背後にあるコードですAllPageViewLoad_PageCollectionクラスのコンストラクターを呼び出します

public async void Load_PageCollection()
{
    m_pageConfig = new PageConfig();
    Page_Collection[] tmppage = await m_pageConfig.Read_FromJSONFile(App.PAGECONFIG);
    List<Page_Collection> tmp;
    if (tmppage != null)
    {
        for (int i = 0; i < tmppage.Length; i++)
        {
            tmppage[i].UpdateCompletionStatus();
        }
        tmp = tmppage.ToList();
        ObservableCollection<Page_Collection> NoteCol = new ObservableCollection<Page_Collection>(tmp.ToList<Page_Collection>());
        PageCollection = NoteCol;
        PageLV.DataContext = PageCollection;
        m_pageManager.InitilizeWithFileLoc(PageCollection.ToArray());
    }
    else
    {
        PageCollection = new ObservableCollection<Page_Collection>();
        PageLV.DataContext = PageCollection;// PageLV is the grid view. The gridview, the image and a stackpanel.
    }
}
4

2 に答える 2

1

更新されたコードの後に​​編集されました。

テンプレートプロパティをINotifyPropertyChangedをサポートする別のクラスに移動してから、各PageCollectionインスタンスからこのクラスを参照する必要があります。

public class PageCollectionTemplate : INotifyPropertyChanged {

   private static readonly void m_Instance=new PageCollectionTemplate();

   private int m_templateWidth;

   public static PageCollectionTemplate Instance {
     get { return m_Instance; }
   }

   public  int TemplateWidth {
        get { return m_templateWidth; }

        set 
        { 
            if (m_templateWidth == value) return;
            m_templateWidth = value;                
            OnPropertyChanged("TemplateWidth");
        }
    }
    // Do same for template height...

   protected void OnPropertyChanged(string propertyName) {
     var handler=PropertyChanged;
     if (handler!=null) handler(this,new PropertyChangedEventArgs(propertyName));
  }

  public event PropertyChangedEventHandler PropertyChanged;

}

更新、私は前にこのビットを逃しました、iあなたのページコレクションクラスで

public PageCollectionTemplate Tempate { 
    get { return PageCollectionTemplate.Instance; }
}

次に、バインディングをに更新します{Binding Template.TemplateWidth}

バインディングのソースをシングルトンインスタンスに設定することで可能な代替実装があります。例:

{Binding TemplateWidth,Source={x:Static w:PageCollectionTemplate.Instance}

ただし、次の名前空間を定義する必要がありますw

通知プロパティの変更イベントは発生しないため、次のことを実装しようとしないでください。

public int TemplateWidth {
   get { return PageCollectionTemplate.Instance.TemplateWidth; }
   set { PageCollectionTemplate.Instance.TemplateWidth=value; }
}
于 2012-10-31T11:49:32.707 に答える
0

TemplateWidthの定義を含むクラスは、INotifyPropertyChangedを実装する必要があり、TemplateWidthのセッターは、PropertyChangedイベントを発生させる必要があります。

public int TemplateWidth
{
    get { return m_templateWidth; }

    set 
    { 
        m_templateWidth = value;    
        if (PropertyChanged!=null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs("TemplateWidth"));
        }            
    }
}

PrismやSimpleMVVMなどのフレームワークを使用すると、このタイプのもののコーディングを簡素化でき、「TemplateWidth」などの定数に依存する必要もなくなります。

于 2012-10-31T11:49:17.340 に答える