2013-03-13 113 views
2

注:我使用Visual Studio 2012WPF數據模板和按鈕

我有以下的DataTemplate(注:有一個TextBlock和Button)

<DataTemplate DataType="{x:Type ctlDefs:myButton}"> 
    <StackPanel> 
     <TextBlock Text="{Binding LabelText}" Margin="10,0,10,0"/> 

     <Button Content="{Binding LabelText}" Margin="0,0,10,0" 
      Width="{Binding ButtonWidth}" 
      Height="35" 
      Command="{Binding ButtonCommand}" 
      Visibility="Visible"      
      /> 
    </StackPanel> 
</DataTemplate> 

我的屏幕動態添加myButton的控件到項控件

<ItemsControl ItemsSource="{Binding CommandButtons, Mode=OneWay}" 
       FlowDirection="LeftToRight" 
       DockPanel.Dock="Right" 
       > 

我第一次渲染視圖中,文本塊顯示爲每個按鍵,但按鍵本身並沒有。如果我隱藏視圖並再次顯示,則按鈕有時會出現(如果CommandButton列表不更改,如果更改,則永不顯示)。

渲染視圖後,我查看了輸出窗口並沒有發生綁定錯誤。

我錯過了什麼。

+0

這是否也發生,如果你不綁定按鈕的寬度? ButtonWidth實現INotifyPropertyChanged嗎? – LPL 2013-03-13 22:13:42

+0

是的。如果我刪除ButtonWidth,它的功能相同。從使用WPF檢查器可以看出,按鈕看起來像是在那裏(爲它保留的空間),並且當它顯示出來時,所有屬性都從第一個加載到第二個加載。 – adondero 2013-03-13 22:50:37

回答

2

我已經敲了一個快速的應用程序來處理您的示例代碼,似乎沒有任何問題與視圖。以下是運行時的樣子。

Looks like this

以下附上我的代碼的情況下,它幫助。 注:我沒有爲簡潔添加一個真正的ICommand對象,我意外地命名爲大寫M,而不是小米爲myButton類喜歡你的例子

ViewModelBase.cs

public abstract class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string propertyName) 
    { 
     this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     var handler = this.PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, e); 
     } 
    } 
} 

public class MainWindowViewModel : ViewModelBase 
{ 
    public MainWindowViewModel() 
    { 
     this.CommandButtons = new ObservableCollection<MyButton>(); 
    } 
    public ObservableCollection<MyButton> CommandButtons { get; private set; } 
} 

App.xaml中。 CS

public partial class App : Application 
{ 
    protected override void OnStartup(StartupEventArgs e) 
    { 
     base.OnStartup(e); 

     var mainvm = new MainWindowViewModel(); 
     var window = new MainWindow 
     { 
      DataContext = mainvm 
     }; 
     window.Show(); 

     mainvm.CommandButtons.Add(new MyButton { ButtonWidth = 50, LabelText = "Button 1" }); 
     mainvm.CommandButtons.Add(new MyButton { ButtonWidth = 50, LabelText = "Button 2" }); 
     mainvm.CommandButtons.Add(new MyButton { ButtonWidth = 50, LabelText = "Button 3" }); 
    } 
} 

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ctlDefs="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <DataTemplate DataType="{x:Type ctlDefs:MyButton}"> 
     <StackPanel> 
      <TextBlock Text="{Binding LabelText}" Margin="10,0,10,0"/> 

      <Button Content="{Binding LabelText}" Margin="0,0,10,0" 
        Width="{Binding ButtonWidth}" 
        Height="35" 
        Command="{Binding ButtonCommand}" 
        Visibility="Visible"      
     /> 
     </StackPanel> 
    </DataTemplate> 
</Window.Resources> 
<Grid> 
    <ItemsControl ItemsSource="{Binding CommandButtons, Mode=OneWay}" 
      FlowDirection="LeftToRight" 
      DockPanel.Dock="Right" 
      /> 
</Grid> 

MyButton.cs

public class MyButton : ViewModelBase 
{ 
    private string _labelText; 

    public string LabelText 
    { 
     get { return this._labelText; } 
     set { this._labelText = value; this.OnPropertyChanged("LabelText"); } 
    } 

    private double _buttonWidth; 

    public double ButtonWidth 
    { 
     get { return _buttonWidth; } 
     set { _buttonWidth = value; this.OnPropertyChanged("ButtonWidth"); } 
    } 

    private ICommand _buttonCommand; 

    public ICommand ButtonCommand 
    { 
     get { return _buttonCommand; } 
     set { _buttonCommand = value; this.OnPropertyChanged("ButtonCommand"); } 
    } 
} 
+0

我有你的代碼在我的機器上工作,有幾個不同的ObservableCollection vs CommandButtons上的列表); ICommand vs DelegateCommand。 我做了這些改變,我的工作仍然不起作用。我在想有其他事情正在發生。 感謝您的幫助,以驗證此方法的正常工作。我會盡量簡化我的大型項目,看看我能得到什麼。 我接受了這個答案,因爲它是「應該」的工作方式。 – adondero 2013-03-14 17:00:47

+0

無法點擊向上箭頭,因爲我的代表不夠高:-( – adondero 2013-03-14 17:07:08

+0

最後縮小了。在我的風格文件,有這個條目 '<樣式的TargetType = 「{x:Type按鈕}」> <! - - - > ' 被註釋掉的線是問題,但不知道爲什麼 – adondero 2013-03-14 19:29:35