PDA

View Full Version : شفاف کردن و انتخاب Background برای ListBox



mojdeh6583
چهارشنبه 26 خرداد 1389, 12:02 عصر
سلام
کسی هست که منو به صورت کاملا مبتدی راهنمایی کنه که چطور بتونم listbox رو شیشه ای یا شفاف کنم یا اینکه
برای listbox ام background قرار بدم

این موضوع جدیدیه و ممکنه به درد خیلی ها بخوره
ضمن اینکه ساختار لیست باکس ها و کنترل های دیگه رو هم که بشه به روشی این جوری شفافشون کرد زیبا تر می کنه

منتظر کمکتون هستم

من تو چند تا سایت خارجی چند تا چیز پیدا کردم ولی از اونجایی که گفتم مبتدی هستم نتونستم استفاده کنم
یکیش اینه که نمیدونم کد رو کجا بذارم و چجوری استفاده کنم:

سایت:
http://www.gamedev.net/community/forums/topic.asp?topic_id=561759

متن:
You would typically have an ItemsControl displaying your Images, let's say a ListBox, then you would create a style for your ListBoxItems that changes the background and border color when it's is selected or the mouse is hovering. Here's a complete sample of what I think you want:

Code behind:
public class ImageItem {
public ImageItem(string title, ImageSource source) {
this.Title = title;
this.Source = source;
}
public string Title { get; set; }
public ImageSource Source { get; set; }
}

public partial class Window1 : Window {
public Window1() {
InitializeComponent();
this.Images = new ObservableCollection<ImageItem>();

this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg")));
this.Images.Add(new ImageItem("pic0", this.GetImageSource(@"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg")));
}

public static readonly DependencyProperty ImagesProperty = DependencyProperty.Register(
"Images", typeof(ObservableCollection<ImageItem>), typeof(Window1)
);

public ObservableCollection<ImageItem> Images {
get { return base.GetValue(ImagesProperty) as ObservableCollection<ImageItem>; }
set { base.SetValue(ImagesProperty, value); }
}

BitmapImage GetImageSource(string path) {
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(path, UriKind.Absolute);
img.EndInit();
return img;
}
}




The Xaml could look like this:

<Window
x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfApplication3"
Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Window.Resources>
<style x:Key="ImageItemsstyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border CornerRadius="4"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="#AAAF"/>
<Setter Property="Background" Value="#2AAF"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="#AAF"/>
<Setter Property="Background" Value="#5AAF"/>
</Trigger>
</style.Triggers>
</style>

<DataTemplate DataType="{x:Type l:ImageItem}">
<DockPanel Margin="4" LastChildFill="True">
<TextBlock Text="{Binding Title}" DockPanel.Dock="Bottom"/>
<Image Source="{Binding Source}" Width="50" Height="50"/>
</DockPanel>
</DataTemplate>

</Window.Resources>

<ListBox Margin="10" ItemsSource="{Binding Images}" ItemContainerstyle="{StaticResource ImageItemsstyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"
Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>

</Window>

There is a style for the ListBoxItem, a DataTemplate for the image-wrapper-class and finally the ListBox that displays it all. There might be an easier way to bind the WrapPanel's width but this works.


یا یکی گفته این کد رو بذار:
ListBox.BackColor = Color.Transparent
خوب اینم کار نکرد.

حجتی نیا
چهارشنبه 26 خرداد 1389, 12:49 عصر
http://www.codeproject.com/KB/vista/textonglass.aspx

goolestan
چهارشنبه 26 خرداد 1389, 13:25 عصر
می تونی خاصیت Opacity فرمت را 10 درصد کم کنی تا شفاف تر شود

mojdeh6583
چهارشنبه 26 خرداد 1389, 13:32 عصر
http://www.codeproject.com/KB/vista/textonglass.aspx
خوب راهنمایی جالبی بود استفادش می کنم ببینم چی میشه
ولی میشه اینو برای لیست باکس فارسی توضیح بدید؟
ممنون میشم
آخه این برای فرم شیشه ای بود

mojdeh6583
چهارشنبه 26 خرداد 1389, 13:34 عصر
می تونی خاصیت Opacity فرمت را 10 درصد کم کنی تا شفاف تر شود
خوب من فرمم رو نمی خوام عوض کنم میخوام تصویری که بک گراند فرمم گذاشتم توی لیست باکسمم دیده بشه و اینطور بی ریخت رنگی نباشه

یا بتونم برای لیست باکسم بک گراند بذارم

با opacity تمام فرم و کنترل هاش شفاف میشن که من اینو نمی خوام

hamidreza20
چهارشنبه 26 خرداد 1389, 13:50 عصر
خوب من فرمم رو نمی خوام عوض کنم میخوام تصویری که بک گراند فرمم گذاشتم توی لیست باکسمم دیده بشه و اینطور بی ریخت رنگی نباشه

یا بتونم برای لیست باکسم بک گراند بذارم

با opacity تمام فرم و کنترل هاش شفاف میشن که من اینو نمی خوام

سلام دوست گرامی
می تونید از Skin استفاده کنید...
التماس دعا...

mojdeh6583
پنج شنبه 27 خرداد 1389, 08:37 صبح
می تونید از Skin استفاده کنید...


منظورتون رو متوجه نمیشم
از کدوم Skin می تونم استفاه کنم که این نتیجه را داشته باشد؟