Windows 8 访问ItemTemplate中的命名项
我有以下情况:Windows 8 访问ItemTemplate中的命名项,windows-8,datatemplate,itemscontrol,winrt-xaml,itemtemplate,Windows 8,Datatemplate,Itemscontrol,Winrt Xaml,Itemtemplate,我有以下情况: <Button Click="ClickHandler">Click Me</Button> <TextBox x:Name="MyInputTextBox" /> <ItemsControl ItemsSource="{Binding}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBox x:Nam
<Button Click="ClickHandler">Click Me</Button>
<TextBox x:Name="MyInputTextBox" />
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="MyRepeatTextBox" Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
点击我
如果MyRepeatTextBox.Text==MyInputTextBox.Text,我想将MyRepeatTextBox.Background的颜色更改为绿色。如果MyRepeatTextBox.Text为空,我想将颜色更改为红色。如何实现按钮单击处理程序?不确定按钮事件是否最适合此操作 由于DataTrigger再次没有被带到WPF世界之外,所以这些都被淘汰了。也没有IMultiValueConverter。行为学现在已经过时了,但是对他们来说有一个很好的方法。我会用这个
public class MatchTextForegroundBehaviour : Behavior<TextBox>
{
private TextBox _attachedElement;
public static readonly DependencyProperty MatchForegroundProperty =
DependencyProperty.Register("MatchForeground", typeof(Brush),
typeof(MatchTextForegroundBehaviour),
new PropertyMetadata(new SolidColorBrush(Colors.Green), OnMatchForegroundChanged));
public static readonly DependencyProperty FallbackForegroundProperty =
DependencyProperty.Register("FallbackForeground", typeof(Brush),
typeof(MatchTextForegroundBehaviour),
new PropertyMetadata(new SolidColorBrush(Colors.Black), OnFallbackForegroundChanged));
public static readonly DependencyProperty TextToMatchProperty =
DependencyProperty.Register("TextToMatch", typeof(string),
typeof(MatchTextForegroundBehaviour),
new PropertyMetadata(null, OnTextToMatchChanged));
public Brush MatchForeground
{
get { return (Brush)GetValue(MatchForegroundProperty); }
set { SetValue(MatchForegroundProperty, value); }
}
public Brush FallbackForeground
{
get { return (Brush)GetValue(FallbackForegroundProperty); }
set { SetValue(FallbackForegroundProperty, value); }
}
public string TextToMatch
{
get { return (string)GetValue(TextToMatchProperty); }
set { SetValue(TextToMatchProperty, value); }
}
/// <summary>
/// Event when the behavior is attached to a element.
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
_attachedElement = AssociatedObject;
if(_attachedElement != null)
_attachedElement.TextChanged += (s,e) => ChangeForeground();
}
private static void OnMatchForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = (MatchTextForegroundBehaviour)d;
behavior.ChangeForeground();
}
private static void OnTextToMatchChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = (MatchTextForegroundBehaviour)d;
behavior.ChangeForeground();
}
private static void OnFallbackForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = (MatchTextForegroundBehaviour)d;
behavior.ChangeForeground();
}
private void ChangeForeground()
{
if (_attachedElement == null) return;
if (string.IsNullOrEmpty(TextToMatch)) return; // change foreground to red?
if (_attachedElement.Text == TextToMatch)
{
_attachedElement.Foreground = MatchForeground;
}
else
{
_attachedElement.Foreground = FallbackForeground;
}
}
}
公共类MatchTextForegroundBehavior:Behavior
{
专用文本框附件;
公共静态只读从属属性MatchForegroundProperty=
从属属性寄存器(“匹配前景”,类型(画笔),
类型(MatchTextForeGroundBehavior),
新的PropertyMetadata(新的SolidColorBrush(Colors.Green),OnMatchForegroundChanged);
公共静态只读DependencyProperty FallbackForegroundProperty=
从属属性寄存器(“FallbackForeground”,类型为(刷子),
类型(MatchTextForeGroundBehavior),
新的PropertyMetadata(新的SolidColorBrush(Colors.Black),OnFallbackForegroundChanged);
public static readonly dependencProperty text到tchproperty=
DependencyProperty.Register(“TextToMatch”,类型为(字符串),
类型(MatchTextForeGroundBehavior),
新属性元数据(null,OnTextToMatchChanged));
公共画笔
{
获取{return(Brush)GetValue(MatchForegroundProperty);}
set{SetValue(MatchForegroundProperty,value);}
}
公共灌木丛
{
获取{return(Brush)GetValue(FallbackForegroundProperty);}
set{SetValue(FallbackForegroundProperty,value);}
}
公共字符串文本匹配
{
获取{return(string)GetValue(TextToMatchProperty);}
设置{SetValue(TextToMatchProperty,value);}
}
///
///将行为附加到元素时发生的事件。
///
受保护的覆盖无效附加()
{
base.onatached();
_attachedElement=关联对象;
如果(_attachedElement!=null)
_attachedElement.TextChanged+=(s,e)=>ChangeForeground();
}
MatchForeGroundChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var行为=(matchTextForegroundBehavior)d;
behavior.ChangeForeground();
}
私有静态void OnTextToMatchChanged(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var行为=(matchTextForegroundBehavior)d;
behavior.ChangeForeground();
}
FallbackForegroundChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var行为=(matchTextForegroundBehavior)d;
behavior.ChangeForeground();
}
私有void ChangeForeground()
{
if(_attachedElement==null)返回;
if(string.IsNullOrEmpty(TextToMatch))返回;//是否将前景更改为红色?
if(_attachedElement.Text==TextToMatch)
{
_attachedElement.前台=匹配前台;
}
其他的
{
_attachedElement.Foreground=FallbackForeground;
}
}
}
还有xaml
<TextBox x:Name="MyRepeatTextBox" Text="{Binding}"
TextToMatch="{Binding Text, ElementName=MyInputTextBox}"
FallbackForeground="Black" MatchForeground="Green" />
如果按钮单击事件是真正的您希望如何执行此操作,您可以尝试以下操作。我没有针对WinRT编译这个,但我认为所有使用的东西都在WinRT中
使用以下ExtensionMethod
internal static class TreeExtensions
{
public static T GetChildElement<T>(this DependencyObject element) where T :FrameworkElement
{
if (element == null) return null;
if(element.GetType() == typeof(T)) return (T)element;
T childAsT = null;
int count = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(element, i);
childAsT = child.GetChildElement<T>();
if (childAsT != null) break;
}
return childAsT;
}
}
内部静态类树扩展
{
公共静态T GetChildElement(此DependencyObject元素),其中T:FrameworkElement
{
if(element==null)返回null;
if(element.GetType()==typeof(T))返回(T)元素;
T childAsT=null;
int count=VisualTreeHelper.GetChildrenCount(元素);
for(int i=0;i
在button click事件中,您将执行以下操作(假设您为ItemsControl指定了ItemsControl的名称:
foreach (var item in itemsControl.Items)
{
var element = itemsControl.ItemContainerGenerator.ContainerFromItem(item);
var textblock = element.GetChildElement<TextBlock>();
if (textblock != null)
{
if (textblock.Text == MyInputTextBox.Text)
textblock.Foreground = new SolidColorBrush(Colors.Green);
else
textblock.Foreground = new SolidColorBrush(Colors.Black);
}
}
foreach(itemsControl.Items中的变量项)
{
var元素=itemsControl.ItemContainerGenerator.ContainerFromItem(item);
var textblock=element.GetChildElement();
if(textblock!=null)
{
if(textblock.Text==MyInputTextBox.Text)
textblock.Foreground=新的SolidColorBrush(Colors.Green);
其他的
textblock.Foreground=新的SolidColorBrush(Colors.Black);
}
}
谢谢!您能从外部按钮的单击处理程序的角度解决这个问题吗?可能吗?