C# Windows Phone 8用户控制图像按钮,具有不同状态的不同图像
我是Windows Phone 8开发的新手(你可以说知情的noob正在寻找答案),我正在寻求有关以下问题的帮助: 我有usercontrol——更具体地说是一个包含图像和文本的按钮。我希望该按钮具有以下属性:C# Windows Phone 8用户控制图像按钮,具有不同状态的不同图像,c#,xaml,user-controls,windows-phone-8,C#,Xaml,User Controls,Windows Phone 8,我是Windows Phone 8开发的新手(你可以说知情的noob正在寻找答案),我正在寻求有关以下问题的帮助: 我有usercontrol——更具体地说是一个包含图像和文本的按钮。我希望该按钮具有以下属性: ContentImageNormal-这将是按钮启用时显示的图像 ContentImageDisabled-当按钮被禁用时,将显示该图像 现在我在项目中添加了usercontrol文件夹UserControls,我可以使用它。我已经为它创建了一个样式,并更改了禁用按钮的backger
- ContentImageNormal-这将是按钮启用时显示的图像
- ContentImageDisabled-当按钮被禁用时,将显示该图像
<UserControls:MainPageButton ContentText="Log In" ContentImageNormal="/ImagePathOrResourceETC.png" ContentImageDisabled="/ImagePathOrResourceETC.png"/>
<UserControl x:Class="ProjectNameSpace.UserControls.MainPageButton">
<Grid x:Name="LayoutRoot">
<Button >
<Button.Content>
<Grid>
...omitted...
<Image x:Name="ContentImageHolder" ... />
<TextBlock x:Name="ContentTextBlock" .../>
</Grid>
</Button.Content>
</Button>
</Grid>
</UserControl>
...usings omitted...
public partial class MainPageButton : UserControl
{
...constructor and text setting omitted for brevity...
public ImageSource ContentImage
{
get
{
return ContentImageHolder.Source;
}
set
{
ContentImageHolder.Source = value;
}
}
}
}
- 如何更改隐藏的代码和所需的其他内容,以便我可以按照以下要求使用它?
<UserControls:MainPageButton ContentText="Log In" ContentImageNormal="/ImagePathOrResourceETC.png" ContentImageDisabled="/ImagePathOrResourceETC.png"/>
<UserControl x:Class="ProjectNameSpace.UserControls.MainPageButton">
<Grid x:Name="LayoutRoot">
<Button >
<Button.Content>
<Grid>
...omitted...
<Image x:Name="ContentImageHolder" ... />
<TextBlock x:Name="ContentTextBlock" .../>
</Grid>
</Button.Content>
</Button>
</Grid>
</UserControl>
...usings omitted...
public partial class MainPageButton : UserControl
{
...constructor and text setting omitted for brevity...
public ImageSource ContentImage
{
get
{
return ContentImageHolder.Source;
}
set
{
ContentImageHolder.Source = value;
}
}
}
}
您可以对图像和文本进行简单的绑定 例如:
<Button name="buttonTest">
<Button.Content>
<Grid>
...omitted...
<Image x:Name="ContentImageHolder" Source="{Binding ElementName=buttonTest, Path=IsEnabled, Converter ={StaticResource convertButton}}" Grid.Row="0" />
<TextBlock x:Name="ContentTextBlock" Text="My Sample Text" Grid.Row="1" FontSize="{StaticResource PhoneFontSizeNormal}" FontFamily="{StaticResource PhoneFontFamilyNormal}" Foreground="{StaticResource PhoneForegroundBrush}" TextAlignment="Center"/>
</Grid>
…省略。。。
然后,您将创建一个转换器,该转换器将作为值接收True或False(已启用或未启用),然后在每个状态下返回所需的图像
你的转换器可能是这样的
public class ButtonImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if((bool) value)
return CreateImage("pack://application:,,,/ButtonEnabled.ico"); // url should be correct
else return CreateImage("pack://application:,,,/ButtonDisable.ico");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
/// <summary>
/// Method to create an image to bind to icons in treeview
/// </summary>
/// <param name="uriToImage">uri to image in folder.</param>
/// <returns>a bitmap image to bind to image source in tree view</returns>
private BitmapImage CreateImage(string uriToImage)
{
if (!string.IsNullOrEmpty(uriToImage))
{
BitmapImage genericBitmap = new BitmapImage();
genericBitmap.BeginInit();
genericBitmap.UriSource = new Uri(uriToImage);
genericBitmap.EndInit();
return genericBitmap;
}
return null;
}
}
公共类按钮图像转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
如果((布尔)值)
返回CreateImage(“pack://application:,,,/ButtonEnabled.ico”);//url应正确
否则返回CreateImage(“pack://application:,,,/ButtonDisable.ico”);
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回null;
}
///
///方法创建要绑定到treeview中图标的图像
///
///文件夹中图像的uri。
///在树状视图中绑定到图像源的位图图像
私有BitmapImage CreateImage(字符串uriToImage)
{
如果(!string.IsNullOrEmpty(uriToImage))
{
BitmapImage genericBitmap=新的BitmapImage();
genericBitmap.BeginInit();
genericBitmap.UriSource=新Uri(uriToImage);
genericBitmap.EndInit();
返回通用位图;
}
返回null;
}
}
对于文本,如果它依赖于任何属性,您也可以这样做。您可以对图像和文本进行简单绑定 例如:
<Button name="buttonTest">
<Button.Content>
<Grid>
...omitted...
<Image x:Name="ContentImageHolder" Source="{Binding ElementName=buttonTest, Path=IsEnabled, Converter ={StaticResource convertButton}}" Grid.Row="0" />
<TextBlock x:Name="ContentTextBlock" Text="My Sample Text" Grid.Row="1" FontSize="{StaticResource PhoneFontSizeNormal}" FontFamily="{StaticResource PhoneFontFamilyNormal}" Foreground="{StaticResource PhoneForegroundBrush}" TextAlignment="Center"/>
</Grid>
…省略。。。
然后,您将创建一个转换器,该转换器将作为值接收True或False(已启用或未启用),然后在每个状态下返回所需的图像
你的转换器可能是这样的
public class ButtonImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if((bool) value)
return CreateImage("pack://application:,,,/ButtonEnabled.ico"); // url should be correct
else return CreateImage("pack://application:,,,/ButtonDisable.ico");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
/// <summary>
/// Method to create an image to bind to icons in treeview
/// </summary>
/// <param name="uriToImage">uri to image in folder.</param>
/// <returns>a bitmap image to bind to image source in tree view</returns>
private BitmapImage CreateImage(string uriToImage)
{
if (!string.IsNullOrEmpty(uriToImage))
{
BitmapImage genericBitmap = new BitmapImage();
genericBitmap.BeginInit();
genericBitmap.UriSource = new Uri(uriToImage);
genericBitmap.EndInit();
return genericBitmap;
}
return null;
}
}
公共类按钮图像转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
如果((布尔)值)
返回CreateImage(“pack://application:,,,/ButtonEnabled.ico”);//url应正确
否则返回CreateImage(“pack://application:,,,/ButtonDisable.ico”);
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回null;
}
///
///方法创建要绑定到treeview中图标的图像
///
///文件夹中图像的uri。
///在树状视图中绑定到图像源的位图图像
私有BitmapImage CreateImage(字符串uriToImage)
{
如果(!string.IsNullOrEmpty(uriToImage))
{
BitmapImage genericBitmap=新的BitmapImage();
genericBitmap.BeginInit();
genericBitmap.UriSource=新Uri(uriToImage);
genericBitmap.EndInit();
返回通用位图;
}
返回null;
}
}
对于文本,如果它依赖于任何属性,您也可以这样做。Jan
对于你的问题,你只是错过了一件事
在UserControl中,必须创建如下所示的DependencyProperties:
public static readonly DependencyProperty ContentImageNormalProperty =
DependencyProperty.Register("ContentImageNormal",
typeof(ImageSource ),
typeof(MainPageButton));
public string ContentImageNormal{ get { return (ImageSource)this.GetValue(ContentImageNormalProperty); }
set { this.SetValue(ContentImageNormalProperty, value); } }
<cc:ThreeWay xmlns:cc="clr-namespace:CustomImageControl.Controls"
ActiveImage="active.png"
InactiveImage="inactive.png"
DerpImage="derp.jpg"
ImageState="{Binding State}" />
那么你的财产应该是这样的:
public static readonly DependencyProperty ContentImageNormalProperty =
DependencyProperty.Register("ContentImageNormal",
typeof(ImageSource ),
typeof(MainPageButton));
public string ContentImageNormal{ get { return (ImageSource)this.GetValue(ContentImageNormalProperty); }
set { this.SetValue(ContentImageNormalProperty, value); } }
<cc:ThreeWay xmlns:cc="clr-namespace:CustomImageControl.Controls"
ActiveImage="active.png"
InactiveImage="inactive.png"
DerpImage="derp.jpg"
ImageState="{Binding State}" />
对要设置的每个属性执行此操作
请参见此示例:
我希望这对你有帮助
问候,,
塞尔吉奥·蒙泰罗
对于你的问题,你只是错过了一件事
在UserControl中,必须创建如下所示的DependencyProperties:
public static readonly DependencyProperty ContentImageNormalProperty =
DependencyProperty.Register("ContentImageNormal",
typeof(ImageSource ),
typeof(MainPageButton));
public string ContentImageNormal{ get { return (ImageSource)this.GetValue(ContentImageNormalProperty); }
set { this.SetValue(ContentImageNormalProperty, value); } }
<cc:ThreeWay xmlns:cc="clr-namespace:CustomImageControl.Controls"
ActiveImage="active.png"
InactiveImage="inactive.png"
DerpImage="derp.jpg"
ImageState="{Binding State}" />
那么你的财产应该是这样的:
public static readonly DependencyProperty ContentImageNormalProperty =
DependencyProperty.Register("ContentImageNormal",
typeof(ImageSource ),
typeof(MainPageButton));
public string ContentImageNormal{ get { return (ImageSource)this.GetValue(ContentImageNormalProperty); }
set { this.SetValue(ContentImageNormalProperty, value); } }
<cc:ThreeWay xmlns:cc="clr-namespace:CustomImageControl.Controls"
ActiveImage="active.png"
InactiveImage="inactive.png"
DerpImage="derp.jpg"
ImageState="{Binding State}" />
对要设置的每个属性执行此操作
请参见此示例:
我希望这对你有帮助
问候,,
塞尔吉奥·蒙泰罗(Sérgio Monteiro)这是一个非常简单的东西(好吧,对我来说,因为我已经为大多数硬东西准备了大量的代码片段),所以这里它是一个自定义用户控件 首先,使用图像创建用户控件。耶
<UserControl x:Class="CustomImageControl.Controls.ThreeWay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
<Image
x:Name="derpImage" />
</UserControl>
就这样。您可以这样使用它:
public static readonly DependencyProperty ContentImageNormalProperty =
DependencyProperty.Register("ContentImageNormal",
typeof(ImageSource ),
typeof(MainPageButton));
public string ContentImageNormal{ get { return (ImageSource)this.GetValue(ContentImageNormalProperty); }
set { this.SetValue(ContentImageNormalProperty, value); } }
<cc:ThreeWay xmlns:cc="clr-namespace:CustomImageControl.Controls"
ActiveImage="active.png"
InactiveImage="inactive.png"
DerpImage="derp.jpg"
ImageState="{Binding State}" />
这假设DataContext是一个实例,其属性名为
State
,类型为ThreeWay.State
。如果没有,可以使用自定义转换器将任何类型(可为null的bool?)转换为正确的类型。这是非常简单的,