2008-10-08 3 views

답변

2

저는 트리거를 통해 또는 Enabled 값을 다른 종속성 속성 (가능하면 부모 요소)에 바인딩하여 xaml 마크 업과 함께 버튼의 Enabled 속성 설정을 트리거하는 것이 Joel에게 동의합니다 ValueConverter의 도움.

그러나 C#에서 명시 적으로해야하는 경우에는 단추의 템플릿 속성에서 FindName() 메서드를 사용할 수 있습니다. MSDN의 'linked here 방법'이 있습니다.

3

정말 명시 적으로해서는 안되며 대신 ControlTemplate에서 트리거로 설정해야합니다. 이러한 트리거는 다른 변경 사항에 반응하여 ButtonEnabled 속성을 설정합니다.

4

내 버튼은 RoutedUICommands를 통해 쉽게 제어 할 수 있습니다 (쉽게 활성화 및 비활성화 할 수 있음). 명령은 Button에 대한 참조를 필요로하지 않으므로 문제가 해결됩니다.

는 여기를 참조하십시오 :

<Window.CommandBindings> 
    <CommandBinding 
     Command="{x:Static local:MyCommands.MyCommand}" 
     Executed="CommandBinding_Executed" 
     CanExecute="CommandBinding_CanExecute" /> 
</Window.CommandBindings> 

<Window.Resources> 
    <ControlTemplate x:Key="MyTemplate" TargetType="Label"> 
     <Button Command="{x:Static local:MyCommands.MyCommand}"> 
      <TextBlock> 
       Click Me 
       <TextBlock Text="{TemplateBinding Content}" /> 
      </TextBlock> 
     </Button> 
    </ControlTemplate> 
</Window.Resources> 

<StackPanel> 
    <Label Template="{StaticResource MyTemplate}">1</Label> 
    <Label Template="{StaticResource MyTemplate}">2</Label> 
    <Label Template="{StaticResource MyTemplate}">3</Label> 
    <Label Template="{StaticResource MyTemplate}">4</Label> 
    <Label Template="{StaticResource MyTemplate}">5</Label> 
</StackPanel> 

사용이 : 나는 controlTemplates & dataTemplates의 버튼에 문제가 있었다

screenshot

0

:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void CommandBinding_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) 
    { 
     // your logic here 
     int _Integer = -1; 
     int.TryParse(e.Parameter.ToString(), out _Integer); 
     e.CanExecute = _Integer % 2 == 0; 
    } 

    private void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) 
    { 
     // do something when clicked 
    } 
} 

public static class MyCommands 
{ 
    public static RoutedUICommand MyCommand = new RoutedUICommand(); 
} 

의 모습. 트리거는 종종 고객이 원하는 것을 정확히 제공하기에는 너무 성가신 것으로 생각됩니다. 너무 많은 변환기로 끝났습니다. 내가 처음 시도한 방법은이 어려운 템플릿을 인라인으로 정의하여 내 화면 ViewModel에서 명령 (& 코드 숨김 이벤트)에 바인딩 될 수 있도록했습니다. (다시 indevidual 파일에

hourHand = this.Template.FindName("PART_HourHand", this) as Rectangle; 

최근하지만, 내가 변경 한 전술 : 당신이 당신의 자신의 제어를하는 주형 경우

것은, 내가 가장 쉬운 방법은 다음과 같은 것을 사용 단순히 버튼을 이름을 지정하는 것입니다 언급해야한다 .. RE-USE!)하지만 좀 더 복잡한 모든 템플릿에 ...TemplateCommandStore 접미사 인 ViewModel (일종의)을 추가하기 시작했습니다. (주로, 일관성) 내 customcontrols을하는 주형 때 난 그때 행복하게 템플릿에서이 명령을 사용할 수 있습니다

public class BookingDetailsTemplateCommandStore 
{ 
    public OpenWindowCommand_Command OpenWindowCommand_Command { get; set; } 

    public BookingDetailsTemplateCommandStore() 
    { 
     OpenWindowCommand_Command = new OpenWindowCommand_Command(this); 
    }  
} 

사용합니다. & 이벤트를 캡처하기 위해 종속성 (?) 속성을 바인딩하여 명령 참조로 명령 참조를 전달할 수도 있습니다.

<DataTemplate DataType="{x:Type LeisureServices:BookingSummary}"> 
    <Border Height="50" otherStyling="xyz"> 
     <Border.Resources> 
      <local:BookingDetailsTemplateCommandStore x:Key="CommandStore" /> 
     </Border.Resources> 
     <DockPanel> 
      <Button otherStyling="xyz" 
        Command="{Binding Source={StaticResource CommandStore}, 
             Path=OpenWindowCommand_Command}" /> 

MVVM : 내가 너무 오래 그래픽 로직 그냥, 비즈니스 로직과는 완전히 별개로,이 방법은 MVVM을 방해하지 않는 것을 발견. 본질적으로 일관성있는 C# 코드 기능을 템플릿에 추가합니다.이 기능을 사용하면 winforms에서 너무 많은 시간을 소비 한 사람이 내가하는 것처럼 많이 놓칠 수 있습니다.

관련 문제