문제

나는 그냥 사용하거나 사용하지 않으려는 버튼 내부에서 테마 리소 WPF 편집기를 사용하는.

도움이 되었습니까?

해결책

Joel은 선호하는 방법이 트리거를 통해 또는 사용 가능한 값을 다른 종속성 속성, 아마도 부모 요소에 바인딩함으로써 XAML 마크 업으로 버튼을 활성화 된 속성의 설정을 트리거하는 것임에 동의합니다. ValueConverter.

그러나 C#에서 명시 적으로 수행 해야하는 경우 버튼의 템플릿 속성에서 FindName () 메소드를 사용할 수 있습니다. MSDN 'How'가 있습니다. 여기에 연결되었습니다.

다른 팁

내가 추천하는 버튼을 쉽게 통제를 통해 RoutedUICommands-을 포함한 쉽게 사용하고 그들을 비활성화합니다.명령에 필요하지 않습의 어떤 유형을 참조하여 버튼 그래서 이 문제를 해결합,too.

는 여기를 참조하십시오:

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

이것을 사용:

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

처럼 보인:

screenshot

당신은 정말로 그것을 명시 적으로하지 말고 대신 트리거로 설정하고 있습니다. ControlTemplate 그 자체. 이 트리거는 다른 변화에 반응하고 있으며 Enabled 의 속성 Button.

ControlTemplates 및 DataTemplates의 버튼에 문제가있었습니다. 나는 트리거가 종종 고객이 원하는 것을 정확하게 제공하기에는 너무 번거 롭다는 것을 알았습니다. 나는 너무 많은 컨버터로 끝났습니다. 내가 처음 시도한 방법은이 더 어려운 템플릿을 인라인으로 정의하여 화면 뷰 모델에서 명령 (& CodeBehind 이벤트)에 결합 할 수 있도록하는 것이 었습니다.

자신의 컨트롤을 템플릿하는 경우 가장 쉬운 방법은 단순히 버튼을 지정 한 다음 다음과 같은 것을 사용하는 것입니다.

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

그러나 최근에 나는 전술을 바꾸었다 (무의미한 파일로 돌아 가기!). ...TemplateCommandStore 조금 복잡해지는 모든 템플릿에. 나는 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에서 너무 많은 시간을 보낸 사람은 내가하는 것만 큼 많이 놓칠 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top