2014-02-17 3 views
0

에서 UserControl에서 (콘텐츠) 나는 내가 다른 XAML 파일 내에서 참조/사용하고있는 다음 UserControl을 가지고 -액세스 된 ControlTemplate XAML

<UserControl x:Class="WpfApplication2.MutuallyExclusiveToggleControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     x:Name="SpecialToggleControl" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.Resources> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <ToggleButton Content="{TemplateBinding ContentControl.Content}" 
         Background="{Binding ElementName=SpecialToggleControl, Path=TileBackground}"   
         IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}" 
            Name="toggleButton"/> 
        <ControlTemplate.Triggers> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 
<ListBox x:Name="_listBox" 
    SelectionChanged="ListBoxSelectionChanged" 
    SelectedItem="{Binding ElementName=SpecialToggleControl, Path=SelectedItem}" 
    SelectionMode="Single" ItemsSource="{Binding ElementName=SpecialToggleControl, Path=ItemsSource}"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Columns="{Binding ElementName=SpecialToggleControl, Path=ColumnCount}"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox> 

는 질문 :가 어떻게 액세스합니까 ToggleButton을의를 이 UserControl을 사용하고있는 곳의 콘텐트 (ControlTemplate에 있음). 예를 들어, 콘텐츠에 따라 배경색을 설정하고 싶습니다. UserControl 내부에서이 작업을 수행하고 싶지 않습니다. 나는이 UserControl을 사용하고있는 곳에서 이것을 얻고 싶습니다.

미리 감사드립니다.

답변

0

좋아, 대답하겠습니다. 그 대신에 대신 ControlTemplate을 사용했습니다. ListBox의 ItemTemplate을 수정하고 동일한 ControlTemplate을 DataTemplate으로 적용해야합니다.가 (변환기 "CC는"지금 조건부 색상을 적용합니다)

   <WpfApplication2:MutuallyExclusiveToggleControl.Resources> 
       <Style TargetType="{x:Type ToggleButton}"> 
        <Style.Setters> 
         <Setter Property="Background" Value="{Binding Converter={StaticResource cc}}"></Setter> 
        </Style.Setters>      
       </Style> 
      </WpfApplication2:MutuallyExclusiveToggleControl.Resources> 

변환기 코드 :

(I이 컨트롤을 사용)

<ListBox x:Name="_listBox" 
    SelectedItem="{Binding ElementName=SpecialToggleControl, Path=SelectedItem}" 
    SelectionMode="Single" 
    ItemsSource="{Binding ElementName=SpecialToggleControl, Path=ItemsSource}"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Columns="{Binding ElementName=SpecialToggleControl, Path=ColumnCount}" Background="Beige"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <ToggleButton Content="{TemplateBinding ContentControl.Content}" 
          IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
              AncestorType={x:Type ListBoxItem}},Path=IsSelected}" 
          Name="toggleButton"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 

    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
      <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
     </Style> 
    </ListBox.ItemContainerStyle> 

</ListBox> 

MainWindow.xaml

public class ColourConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if((string)value == "Buy") { return Brushes.Blue; } if ((string)value == "Sell") { return Brushes.Red; } return Brushes.White; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 
0

UserControl을 사용하는 곳에서 액세스하려는 속성에 대한 종속성 속성을 만들 수 있습니다. 이를 통해 외부의 값을 ControlTemplate의 특정 속성에 바인딩 할 수 있습니다.

같은 것을 보일 것이다 외부에서 버튼의 내용을 설정하기위한 코드를 입력하십시오 : 당신이 그것을 결합 후 당신의 UserControl의 속성에

public string ButtonContent 
{ 
    get { return (string)GetValue(ButtonContentProperty); } 
    set { SetValue(ButtonContentProperty, value); } 
} 

public static readonly DependencyProperty ButtonContentProperty = 
    DependencyProperty.Register("ButtonContent", typeof(string), typeof(TestButton), new PropertyMetadata("Test")); 

: (당신의 UserControl의 코드 숨김에서)

종속성 속성을 당신이 그런 식으로 당신의 UserControl을 만들고 예를 들어 당신의 ViewModel에서 원하는 컨텐츠를 취할 수 (당신이 MVVM을 사용하는 가정) : 이

<wpfApplication2:TestButton ButtonContent="{Binding TestContentFromViewModel}"></wpfApplication2:TestButton> 

당신은 당신의 UserControl의 모든 속성에 대해 그렇게 할 수 네가 원해.

종속성 속성에 대한 자세한 내용은 here을 참조하십시오.

+0

그러나 ** Content **는 ListBox의 ** ItemsSource **를 기반으로 설정됩니다 (볼 수있는 경우 e xaml). 콘텐츠를 별도로 설정하고 싶지 않습니다. –