我有一個包含各種資訊的類,如標題、影像源或描述。
為了在串列框中顯示這些,我使用了資料模板。這作業正常。
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Titel}" />
<Label Content="{Binding Beschreibung}"/>
<Label >
<AccessText Text="{Binding Version}"/>
</Label>
<Label Style="{DynamicResource ResourceKey={Binding Resource}}"/>
</StackPanel>
</DataTemplate>
在每個串列框元素后面,我想顯示三個標簽之一。我嘗試使用標簽中 x:key 的名稱來定義每個標簽的樣式。(這里:標簽更新)
<Label Style="{DynamicResource ResourceKey={Binding Resource}}"/>
標簽樣式:
<Style x:Key="Labelupdate" TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Content" Value="Update"/>
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="30"/>
<Setter Property="FontFamily" Value="{StaticResource Roboto}"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="{DynamicResource FntBtnColor}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border CornerRadius="2" Background="DeepSkyBlue" >
<DockPanel>
<Image Source="Images/refresh.png" Height="15" Width="15" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="25 0 0 0"/>
<ContentPresenter x:Name="contentPresenterBtn" HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" RecognizesAccessKey="True" Margin="0 0 10 0"/>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
將專案添加到串列框的示例代碼:
for (int i = 1; i < 6; i )
{
items.Add(new ListBxItem("Beschreibung", i " Titel", "Version", "Labelupdate"));
}
InstallListbox.ItemsSource = items;
不幸的是,這不能如我所愿,經過幾個小時的搜索,我現在沒有找到解決方案。我在 Wpf 和 C# 中還是比較新的。
uj5u.com熱心網友回復:
恐怕您無法將某些東西系結到ResourceKey.
修改StyleorControlTemplate以包含觸發器以根據 的某些屬性更改屬性ListBxItem,例如:
<DataTemplate>
...
<Label>
<Label.Style>
<Style TargetType="Label">
...
<Style.Triggers>
<DataTrigger Binding="{Binding Resource}" Value="Labelupdate">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513561.html
標籤:C#wpfxml
