如何为静态属性设置转换器?以下示例显示了我的问题–我想将文本转换为大写。TextBlock
<UserControl x:Class="CoRiMaCorporate.HomeScreen.Controls.Home.ConfigurationControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mainResources="clr-namespace:MainSharedResources;assembly=MainSharedResources"
xmlns:converters="clr-namespace:CommonClientLibrary.Converters;assembly=CommonClientLibrary" />
<UserControl.Resource>
<converters:StringToUpperCaseStringConverter x:Key="stringToUpperCaseStringConverter" />
</UserControl.Resource>
<Grid>
…
<TextBlock Text="{x:Static mainResources:Lang.Applications}" />
…
</Grid>
Run Code Online (Sandbox Code Playgroud)
我在寻找这样的东西:
<TextBlock Text="{Binding Converter=stringToUpperCaseStringConverter, ConverterParameter={x:Static mainResources:Lang.Applications}}" />
Run Code Online (Sandbox Code Playgroud) 此答案为可观察列表提供了解决方案,如果列表元素的属性发生变化,该列表将发送“列表已更新” 通知。
就我而言,这种可观察列表的元素(一个Element 类)很复杂,我不喜欢为每个成员变量实现属性。因此,我在Element 类中添加了一个BooleanProperty来指示类的变化。
元素类
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
public class Element {
// ...
private ReadOnlyBooleanWrapper changeIndicatorWrapper;
public Element() {
//...
changeIndicatorWrapper = new ReadOnlyBooleanWrapper(false);
}
public ReadOnlyBooleanProperty changeIndicatorProperty() {
return changeIndicatorWrapper.getReadOnlyProperty();
}
public void someMethod() {
// Some update
changeIndicatorWrapper.set(!changeIndicatorWrapper.get());
}
}
Run Code Online (Sandbox Code Playgroud)
可观察列表
ObservableList<Element> elementsObservableList = FXCollections.observableList(
new ArrayList<>(),
(Element element) -> new Observable[] { element.changeIndicatorProperty() }
);
elementsObservableList.addListener(new ListChangeListener<Element>() {
@Override …Run Code Online (Sandbox Code Playgroud) 对于自定义DataTemplateSelector,我使用了ContentProperty属性,该属性应表示DataTemplates的集合。
\n\n[ContentProperty(nameof(Templates))]\npublic class CustomTemplateSelector : DataTemplateSelector\n{\n public List<DataTemplate> Templates { get; } = new List<DataTemplate>();\n\n public override DataTemplate SelectTemplate(object item, DependencyObject container)\n {\n /* \xe2\x80\xa6 */\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nXAML中的用法:
\n\n<ItemsControl ItemsSource="{Binding MyCollection}">\n <ItemsControl.ItemTemplateSelector>\n <local:CustomTemplateSelector>\n <DataTemplate DataType="{x:Type system:Boolean}">\n <Label>BOOL</Label>\n </DataTemplate>\n <DataTemplate DataType="{x:Type system:String}">\n <Label>STRING</Label>\n </DataTemplate>\n </local:CustomTemplateSelector>\n </ItemsControl.ItemTemplateSelector>\n</ItemsControl>\nRun Code Online (Sandbox Code Playgroud)\n\n我的问题是为什么我不能使用IListorICollection数据类型而不是List数据类型。
如果我使用这些类型,则会收到编译错误Cannot set content property \'Templates\' on element \'CustomTemplateSelector\'。“模板”的访问级别不正确或其程序集不允许访问。
\n由于缺少教程和以下问题的示例不完整,我决定写这个问题。如果这个问题的答案成为解决类似问题的有效例子,我会很高兴。
任务
让我们使用JavaFX技术(使用FXML作为该技术的一部分来制作图形视图)制作一个GUI 应用程序,例如显示用户集合和每个用户的汽车集合。让我们还使用JavaFX 属性和bindig机制将模型(数据)与 GUI 同步。
执行
我从为用户和汽车创建类开始。
用户.java
package example;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class User {
public StringProperty firstName;
public StringProperty lastName;
private ObservableList<Car> cars;
public User(String firstName, String lastName, Car[] cars) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.cars = FXCollections.observableArrayList(cars);
}
public String getFirstName() …Run Code Online (Sandbox Code Playgroud)