我正在将 8.1 Universal 转换为 UWP。我在代码隐藏中有一些代码,可以通过 View 模型数据以编程方式在堆栈面板中创建可能有数百个节点。我正在尝试向其中一些节点添加一些视觉响应规则。
我使用的模式基本上是:
foreach (var item in ViewModel.items)
{
var fooStackPanel = CreateItemRowStackPanel();
fooStackPanel.Children.Add(CreateItemImage(item.ProductUrl));
fooStackPanel.Children.Add(CreateItemNameTextBlock(item.Name));
// ...
LayoutGrid.Children.Add(fooStackPanel);
}
我想在其中某处为特定节点添加响应规则。
下面是我想要执行的操作在 XAML 中的显示方式,但我需要在后面的代码中执行这些 setter 。我该怎么做?
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WindowStates">
<VisualState x:Name="NarrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<!-- How do I add these programmatically in the code behind? -->
<Setter Target="Item1Image.Visibility"
Value="Collapsed" />
<Setter Target="Item2Image.Visibility"
Value="Collapsed" />
<!-- ... -->
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
最佳答案
正如 Bart 所说,绑定(bind)可见性要容易得多,但如果由于某种原因这对您不起作用,您可以添加 VisualState
Setters
通过这种方式从代码隐藏:
1)创建一个新的视觉状态组
var vsg = new VisualStateGroup();
2) 创建你的状态
var vs = new VisualState()
你不能说出它的名字,但也没有必要这样做。如果你真的必须(例如,你想手动使用 GoToState
,你可以这样做
var vs = (VisualState) XamlReader.Load($"<VisualState x:Name=\"{*yournameinline*}\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" />");
.
3) 添加你的状态触发器:
vs.StateTriggers.Add(new AdaptiveTrigger
{
MinWindowWidth = 0.0
});
4) 添加你的二传手:
vs.Setters.Add(new Setter
{
Target = new TargetPropertyPath
{
Path = new PropertyPath("(Image.Visibility)"),
Target = *yourimage*
},
Value = Visibility.Collapsed
});
5) 获取控件的视觉状态组并添加所有内容:
vsg.States.Add(vs);
VisualStateManager.GetVisualStateGroups(*yourcontrol*).Add(vsg)
关于c# - 以编程方式在代码隐藏中添加视觉状态 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34050213/