3

注意:我使用的是 Visual Studio 2012

我有以下 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 的控件动态添加到 Items Control

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

第一次渲染视图时,每个按钮都会显示 Textblock,但 Button 本身不会。如果我隐藏视图然后再次显示它,按钮有时会出现(如果 CommandButtons 列表没有改变,如果它改变了,它永远不会显示)。

渲染视图后,我查看了输出窗口并且没有绑定错误。

我错过了什么。

4

1 回答 1

4

我已经启动了一个快速应用程序来处理您的示例代码,并且该视图似乎没有任何问题。下面是运行时的样子。

看起来像这样

我在下面包含了我的代码以防万一。注意:为简洁起见,我没有添加真正的 ICommand 对象,并且我不小心将 MyButton 类命名为大写 M 而不是像您的示例那样小 m

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; }
}

应用程序.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" });
    }
}

主窗口.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>

我的按钮.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"); }
    }
}
于 2013-03-14T08:49:49.740 回答