2013-03-13 106 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