2013-03-13 4 views
2

참고 : 나는 비주얼 스튜디오를 사용하고 2012WPF 데이터 템플릿 및 버튼

나는 다음과 같은 DataTemplate을 (참고 : TextBlock의 및 버튼이 있습니다)이

<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" 
       > 

처음으로보기를 렌더링하면 Textblock이 각 단추에 표시되지만 단추 자체는 표시되지 않습니다. 보기를 숨긴 다음 다시 표시하면 단추가 때때로 나타납니다 (CommandButtons 목록이 변경되지 않으면 변경되지 않으면 표시되지 않음).

뷰를 렌더링 한 후에 출력 창을 살펴 보았습니다. 바인딩 오류는 없습니다.

실종 신고입니다.

+0

이 또한 발생합니까? ButtonWidth는 INotifyPropertyChanged를 구현합니까? – LPL

+0

예. ButtonWidth를 제거하면 같은 방식으로 작동합니다. WPF 검사기를 사용하여 말할 수있는 것부터 단추는 보이는 것처럼 보입니다 (공간이 예약되어 있음). 표시되는 첫 번째 하중부터 두 번째 하중까지 모든 속성이 일치합니다. – adondero

답변

2

예제 코드로 작업 할 수있는 빠른 응용 프로그램을 작성 했으므로보기에 문제가없는 것으로 보입니다. 아래는 실행했을 때의 모습입니다.

Looks like this

나는 도움이 경우 아래에있는 내 코드를 포함 시켰습니다. 참고 : 나는 간결에 대한 실제 ICommand의 개체를 추가하지 않았고 실수로 귀하의 예제처럼

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

을 자본 M 대신 작은 m로 MyButton에 클래스를 지명했다. 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 대 ​​ListButtons의 두 가지 차이점이 있습니다.); ICommand 대 DelegateCommand입니다. 이러한 변경 사항을 적용하고 광산이 아직 작동하지 않습니다. 다른 일이 일어나고 있다고 생각하고 있습니다. 이 접근 방식이 작동하는지 확인하는 데 도움을 주셔서 감사합니다. 나는 더 큰 프로젝트를 단순화하고 내가 어디에서 나가는지 보려고 노력할 것이다. 답변으로 받아 들였습니다. 왜냐하면 "어떻게 작동해야 하는가"때문입니다. – adondero

+0

내 담당자가 충분히 높지 않으므로 위쪽 화살표를 클릭 할 수 없습니다 .-( – adondero

+0

마지막으로 좁혀졌습니다.내 스타일 파일에서이 항목이 있었다 '<스타일은 TargetType = "{X : 입력 버튼}"> < - -> ' – adondero