Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

c# - Passing a global value to DateTemplate in WPF

I have the following ListView with a DataTemplate that creates three TextBlocks and populates each entry with the data from class Item.

I want to set the width of each TextBlock to some value that is passed through with the ICollection<Item> array as a value that is the same for each entry. With the syntax below, each Item would have to have the values of GlobalWidth1, etc to be set for each instance.

Is there any way to pass the width values as a global values for the entire ICollection<Item> collection in WPF?

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock Text="{Binding Data1}" Width="{Binding GlobalWidth1}" />
                <TextBlock Text="{Binding Data2}" Width="{Binding GlobalWidth2}" />
                <TextBlock Text="{Binding Data3}" Width="{Binding GlobalWidth3}" />
            </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public class Item
    {
        public string Data1 { get; set; }

        public string Data2 { get; set; }

        public string Data3 { get; set; }
    }
question from:https://stackoverflow.com/questions/65853287/passing-a-global-value-to-datetemplate-in-wpf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of using bindings you could define resource values within the XAML file itself and use {StaticResource ...}.

xmlns:system="clr-namespace:System;assembly=System.Runtime"
...
<ListView>
   <ListView.Resources>
       <system:Double
           x:Key="GlobalWidth1">
           100
       </system:Double>
       <system:Double
           x:Key="GlobalWidth2">
           120
       </system:Double>
       <system:Double
           x:Key="GlobalWidth3">
           150
       </system:Double>
   </ListView.Resources>
   <ListView.ItemTemplate>
       <DataTemplate>
           <WrapPanel>
               <TextBlock Text="{Binding Data1}" Width="{StaticResource GlobalWidth1}" />
               <TextBlock Text="{Binding Data2}" Width="{StaticResource GlobalWidth2}" />
               <TextBlock Text="{Binding Data3}" Width="{StaticResource GlobalWidth3}" />
           </WrapPanel>
       </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

You could even have these defined in your top level App.Resources and set them from the App.xaml.cs.

Note: If you add these to the App.Resources you will need to remove them from the local ListView.Resources

    public App()
    {
        //hardcode

        this.Resources.Add("GlobalWidth1", 100);
        this.Resources.Add("GlobalWidth2", 120);
        this.Resources.Add("GlobalWidth3", 150);

        //or perhaps define them in the global settings
        this.Resources.Add("GlobalWidth1", Settings.Default.GlobalWidth1);
        this.Resources.Add("GlobalWidth2", Settings.Default.GlobalWidth2);
        this.Resources.Add("GlobalWidth3", Settings.Default.GlobalWidth3);
    }

enter image description here

Of course if you want to keep it using bindings you can just add static properties on the Item class:

public class Item
{
    public string Data1 { get; set; }

    public string Data2 { get; set; }

    public string Data3 { get; set; }

    public static double GlobalWidth1 => 100;
    public static double GlobalWidth2 => 120;
    public static double GlobalWidth3 => 150;
}

I would personally recommend keeping it in the XAML for I find it more organized to keep pure UI code in the View layer and out of the ViewModel layer (If you are sticking to MVVM)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...