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 コントロールに追加しています

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

ビューを初めてレンダリングすると、ボタンごとに Textblock が表示されますが、Button 自体は表示されません。ビューを非表示にしてから再度表示すると、ボタンが表示されることがあります (CommandButtons リストが変更されない場合、変更された場合は表示されません)。

ビューをレンダリングした後、出力ウィンドウを確認しましたが、バインディング エラーはありませんでした。

何が欠けていますか。

4

1 に答える 1

4

サンプル コードで動作する簡単なアプリケーションを作成しましたが、ビューに問題はないようです。以下、実行時の様子です。

こんな感じ

役立つ場合に備えて、以下にコードを含めました。注:簡潔にするために実際の ICommand オブジェクトを追加しなかったため、例のように小さな m ではなく大文字の 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"); }
    }
}
于 2013-03-14T08:49:49.740 に答える