저는 사용중인 WPF 편집기에 대한 ControlTemplate 내의 버튼을 활성화 또는 비활성화하려고합니다.ControlTemplate 안에 버튼이 있습니다. 어떻게 참조 할 수 있습니까?
답변
저는 트리거를 통해 또는 Enabled 값을 다른 종속성 속성 (가능하면 부모 요소)에 바인딩하여 xaml 마크 업과 함께 버튼의 Enabled 속성 설정을 트리거하는 것이 Joel에게 동의합니다 ValueConverter의 도움.
그러나 C#에서 명시 적으로해야하는 경우에는 단추의 템플릿 속성에서 FindName() 메서드를 사용할 수 있습니다. MSDN의 'linked here 방법'이 있습니다.
정말 명시 적으로해서는 안되며 대신 ControlTemplate
에서 트리거로 설정해야합니다. 이러한 트리거는 다른 변경 사항에 반응하여 Button
의 Enabled
속성을 설정합니다.
내 버튼은 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의 버튼에 문제가 있었다
:
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에서 너무 많은 시간을 소비 한 사람이 내가하는 것처럼 많이 놓칠 수 있습니다.
- 1. 버튼 안에 플래시 버튼이 있습니다.
- 2. ControlTemplate 내의 컨트롤을 참조 할 수 없습니다.
- 3. 컨트롤 템플릿 안의 요소를 어떻게 참조 할 수 있습니까?
- 4. Javadoc에서 어노테이션을 어떻게 참조 할 수 있습니까?
- 5. 참조 문서에서 양식 데이터를 어떻게 참조 할 수 있습니까?
- 6. jquery에서 어떻게 현재 양식을 클릭하여 클릭하여 참조 할 수 있습니다
- 7. WPF TabControl ControlTemplate
- 8. 어떻게 해시 안에 배열을 초기화 할 수 있습니까?
- 9. strings.xml에서 다른 문자열을 참조 할 수 있습니까?
- 10. 변수에 이름이있는 경우 어떻게 JavaScript 객체를 참조 할 수 있습니까?
- 11. jQuery serialScroll의 인스턴스에서 '활성'항목을 어떻게 참조 할 수 있습니까?
- 12. JavaScript에서는 함수를 호출 한 버튼을 어떻게 참조 할 수 있습니까?
- 13. jquery 플러그인에서 subject $ (this)를 어떻게 참조 할 수 있습니까?
- 14. 스칼라에서 생성자는 생성하는 객체를 어떻게 참조 할 수 있습니까?
- 15. 어떻게 WebDriver의 참조 변수를 초기화 할 수 있습니까?
- 16. 이름에 공백이있는 열을 어떻게 참조 할 수 있습니까?
- 17. 다른 환경을 Symfony의 ProjectConfiguration.class.php에서 어떻게 참조 할 수 있습니까?
- 18. TextArea의 htmlText 블록의 일부만 어떻게 참조 할 수 있습니까?
- 19. ASP.Net 컨트롤에서 일반 HTML 컨트롤을 어떻게 참조 할 수 있습니까?
- 20. WEB-INF에있는 client_deploy.wsdd 파일을 어떻게 참조 할 수 있습니까?
- 21. Rails의 Activerecord 테이블은 어떻게 자체를 참조 할 수 있습니까?
- 22. 스타일에서 스타일을 적용한 요소를 어떻게 참조 할 수 있습니까?
- 23. 어떻게 Perl에 참조 목록을 저장할 수 있습니까?
- 24. ReverseTechner a ControlTemplate
- 25. DOM을로드 할 때까지 제출 버튼이 제출되지 않도록 할 수 있습니까?
- 26. CollapsiblePanelExtender가 ListView 안에 있습니다.
- 27. 전쟁 작업에서 classpathref를 어떻게 참조 할 수 있습니까?
- 28. Win32가 존재하지 않습니다. 어떻게 선언하거나 참조 할 수 있습니까?
- 29. jQuery에서 추가 된 항목을 어떻게 참조 할 수 있습니까?
- 30. .net 실행 파일의 클래스를 어떻게 참조 할 수 있습니까?
XAML을 보거나 사용자의 의도를 파악하는 등 더 구체적인 정보를 알면보다 구체적으로 대답 할 수 있습니다. –